﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Public;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYDuctConnect
	{
		public bool NewTakeOffFitting(IList<Duct> ducts)
		{
			bool flag = false;
			bool result;
			try
			{
				if (ducts.Count < 2)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
				if (xyz != null)
				{
					Connector connector = null;
					Duct duct = null;
					if (hypiping.IsPointOnPipingCurve(xyz))
					{
						connector = hypiping2.GetAppropriateConnector(xyz);
						duct = hypiping.Piping;
					}
					else if (hypiping2.IsPointOnPipingCurve(xyz))
					{
						connector = hypiping.GetAppropriateConnector(xyz);
						duct = hypiping2.Piping;
					}
					if (connector != null && duct != null && !connector.IsConnected)
					{
						document.Create.NewTakeoffFitting(connector, duct);
						flag = true;
					}
				}
				result = flag;
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw new Exception("创建管件连接失败！");
			}
			return result;
		}

		public FamilyInstance NewTakeOffFitting(IList<Duct> ducts, IList<XYZ> xyzs, FamilySymbol fs)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count < 2 || xyzs.Count < 2)
				{
					throw new System.ArgumentNullException("Duct or XYZ", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
				hypiping.ChangeFirstTakeOff(fs);
				hypiping2.ChangeFirstTakeOff(fs);
				document.Regenerate();
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
				if (xyz != null)
				{
					Duct duct = null;
					Connector connector = null;
					if (hypiping.IsPointOnPipingCurve(xyz))
					{
						duct = hypiping.Piping;
						connector = hypiping2.GetAppropriateConnector(xyz);
					}
					else if (hypiping2.IsPointOnPipingCurve(xyz))
					{
						duct = hypiping2.Piping;
						connector = hypiping.GetAppropriateConnector(xyz);
					}
					if (duct == null || connector == null || connector.IsConnected)
					{
						throw new Exception("风管已经连接");
					}
					familyInstance = document.Create.NewTakeoffFitting(connector, duct);
				}
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FamilyInstance NewTakeOffFitting(IList<Duct> ducts, IList<XYZ> xyzs, FamilySymbol fs, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 2 || xyzs.Count != 2)
				{
					connMSG = "请选择两条风管进行侧连接！";
					result = familyInstance;
				}
				else
				{
					Autodesk.Revit.DB.Document document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					if (hypiping.Piping.UniqueId.Equals(hypiping2.Piping.UniqueId))
					{
						connMSG = "同一根风管不能构成侧连接！";
						result = familyInstance;
					}
					else
					{
						YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
						YJKLine hyline2 = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
						if (!hyline.IsVertical(hyline2.Line))
						{
							connMSG = "两风管必须为垂直关系！";
							result = familyInstance;
						}
						else if (fs == null)
						{
							this.NewSomeConnect(ducts);
							result = null;
						}
						else
						{
							bool flag = hypiping.ChangeFirstTakeOff(fs);
							bool flag2 = hypiping2.ChangeFirstTakeOff(fs);
							if (!flag || !flag2)
							{
								connMSG = "设置侧连接方式的首选连接项失败！";
								result = familyInstance;
							}
							else
							{
								document.Regenerate();
								XYZ xyz = hypiping.IntersectionPoint(true, hypiping2.Piping);
								if (xyz == null)
								{
									connMSG = "支风管与主风管没有交点，\n或者交点不在主风管上\n（即第一根选择的风管）,\n无法进行连接操作！";
									result = familyInstance;
								}
								else
								{
									Duct duct = null;
									Connector connector = null;
									if (hypiping.IsPointOnPipingCurve(xyz))
									{
										duct = hypiping.Piping;
										connector = hypiping2.GetAppropriateConnector(xyz);
									}
									else if (hypiping2.IsPointOnPipingCurve(xyz))
									{
										duct = hypiping2.Piping;
										connector = hypiping.GetAppropriateConnector(xyz);
									}
									if (duct == null || connector == null)
									{
										connMSG = "风管或者连接口丢失，\n无法连接操作！";
										result = familyInstance;
									}
									else if (connector.IsConnected)
									{
										connMSG = "风管已经连接！";
										result = familyInstance;
									}
									else
									{
										familyInstance = document.Create.NewTakeoffFitting(connector, duct);
										result = familyInstance;
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool NewElbowFitting(IList<Duct> ducts)
		{
			bool flag = false;
			bool result;
			try
			{
				if (ducts.Count < 2)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
				if (xyz != null)
				{
					Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hypiping2.GetAppropriateConnector(xyz);
					if (appropriateConnector != null && appropriateConnector2 != null && !appropriateConnector.IsConnected && !appropriateConnector2.IsConnected)
					{
						document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
						flag = true;
					}
				}
				document.Regenerate();
				result = flag;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败，可能由于管道角度过大或者过小！");
			}
			return result;
		}

		public bool NewElbowFitting(IList<Duct> ducts, ref string connMSG)
		{
			bool result;
			try
			{
				if (ducts.Count != 2)
				{
					connMSG = "请选择两条风管，再进行弯头连接！";
					result = false;
				}
				else
				{
					Autodesk.Revit.DB.Document document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
					XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
					if (xyz == null)
					{
						connMSG = "进行连接的两风管，没有交点，无法连接！";
						result = false;
					}
					else
					{
						Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
						Connector appropriateConnector2 = hypiping2.GetAppropriateConnector(xyz);
						if (appropriateConnector == null || appropriateConnector2 == null)
						{
							connMSG = "找不到风管连接口，无法连接！";
							result = false;
						}
						else if (appropriateConnector.IsConnected || appropriateConnector2.IsConnected)
						{
							connMSG = "风管已经连接！";
							result = false;
						}
						else
						{
							document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
							bool flag = true;
							document.Regenerate();
							result = flag;
						}
					}
				}
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败，可能由于管道角度过大或者过小！");
			}
			return result;
		}

		public string CheckTerminalBeCovered(Duct duct, FamilyInstance terminal, ref int connType, bool beOnConn = true)
		{
			List<string> promptList = this.GetPromptList(beOnConn);
			YJKPiping<Duct> hyDuct = new YJKPiping<Duct>(duct);
			Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, terminal);
            if (beOnConn && (int)elementConnector.Shape != 1)
			{
				return "贴管连接目前不支持圆形风口。";
			}
			if (!this.JudgeFamilyInstanceConn(hyDuct, elementConnector, ref connType))
			{
				return promptList[0];
			}
			int num = connType;
			string result;
			if (num != 11)
			{
				if (num != 22)
				{
					if (num != 33)
					{
						result = promptList[0];
					}
					else
					{
						result = this.JudgeSideDuctCover(elementConnector, hyDuct, true, beOnConn);
					}
				}
				else
				{
					result = this.JudgeSideDuctCover(elementConnector, hyDuct, false, beOnConn);
				}
			}
			else
			{
				result = this.JudgeEndDuctCover(elementConnector, hyDuct, true);
			}
			return result;
		}

		public void MoveTerminal(Duct duct, FamilyInstance terminal, int connType)
		{
			Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, terminal);
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			YJKLine hyline = new YJKLine(origin, origin2);
			hyline.MakeUnBound();
			double num = hypiping.GetDiameter() * 1.5;
			XYZ xyz = null;
			if (connType != 11)
			{
				if (connType == 22 || connType == 33)
				{
					xyz = hyline.GetProject(elementConnector.Origin).Add(elementConnector.CoordinateSystem.BasisZ * -num);
				}
			}
			else
			{
				Connector connector = (origin.DistanceTo(elementConnector.Origin) > origin2.DistanceTo(elementConnector.Origin)) ? hypiping.EndConnector.Connector : hypiping.StartConnector.Connector;
				xyz = connector.Origin.Add(connector.CoordinateSystem.BasisZ * num);
			}
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			XYZ moveVector = xyz.Subtract(elementConnector.Origin);
			hyrotateFamilyInstance.MoveElem(moveVector, terminal);
		}

		private List<string> GetPromptList(bool beOnConn)
		{
			List<string> list = new List<string>();
			string item = string.Empty;
			string item2 = string.Empty;
			if (beOnConn)
			{
				item = "风口位置不符合贴管连接条件，请调整风口位置。";
				item2 = "风口尺寸不符合贴管连接条件，请调整风口或风管尺寸。";
			}
			else
			{
				item = "风口位置不符合三通或直接连接条件，请调整风口位置。";
				item2 = "";
			}
			list.Add(item);
			list.Add(item2);
			return list;
		}

		private string JudgeEndDuctCover(Connector fiConn, YJKPiping<Duct> hyDuct, bool beOnConn = true)
		{
			List<string> promptList = this.GetPromptList(beOnConn);
			XYZ origin = fiConn.Origin;
			XYZ origin2 = hyDuct.StartConnector.Origin;
			XYZ origin3 = hyDuct.EndConnector.Origin;
			Connector connector = (origin2.DistanceTo(origin) > origin3.DistanceTo(origin)) ? hyDuct.EndConnector.Connector : hyDuct.StartConnector.Connector;
			double num = 0.0001;
			if (!fiConn.CoordinateSystem.BasisZ.IsAlmostEqualTo(connector.CoordinateSystem.BasisZ * -1.0, num))
			{
				return promptList[0];
			}
			YJKLine hyline = new YJKLine(origin2, origin3);
			hyline.MakeUnBound();
			if ((fiConn.Width > connector.Width || fiConn.Height > connector.Height) && !hyline.IsPointOnCurveLine(origin))
			{
				return promptList[1];
			}
			XYZ project = hyline.GetProject(origin);
			XYZ xyz = new XYZ(project.X, project.Y, origin.Z);
			double num2 = origin.DistanceTo(xyz);
			double num3 = xyz.DistanceTo(project);
			double num4 = (connector.Width - fiConn.Width) / 2.0;
			if ((connector.Height - fiConn.Height) / 2.0 < num3 || num4 < num2)
			{
				return promptList[0];
			}
			return "";
		}

		private string JudgeSideDuctCover(Connector fiConn, YJKPiping<Duct> hyDuct, bool beDown = false, bool beOnConn = true)
		{
			List<string> promptList = this.GetPromptList(beOnConn);
			Connector connector = hyDuct.EndConnector.Connector;
			if (beOnConn)
			{
				if (beDown)
				{
					if (connector.Width < fiConn.Height)
					{
						return promptList[1];
					}
				}
				else if (connector.Height < fiConn.Height)
				{
					return promptList[1];
				}
			}
			XYZ origin = fiConn.Origin;
			XYZ origin2 = hyDuct.StartConnector.Origin;
			XYZ origin3 = hyDuct.EndConnector.Origin;
			YJKLine hyline = new YJKLine(origin2, origin3);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(origin);
			XYZ xyz = new XYZ(project.X, project.Y, origin.Z);
			double num = origin.DistanceTo(xyz);
			double num2 = xyz.DistanceTo(project);
			if (beOnConn)
			{
				double num3 = (connector.Width - fiConn.Height) / 2.0;
				double num4 = (connector.Height - fiConn.Height) / 2.0;
				if (beDown)
				{
					if (num > num3 * 1.05)
					{
						return promptList[0];
					}
				}
				else if (num2 > num4 * 1.05)
				{
					return promptList[0];
				}
			}
			else if (connector.Shape == ConnectorSharp.Round && fiConn.Shape == ConnectorSharp.Round)
			{
				double num5 = connector.Radius - fiConn.Radius;
				if (num > num5 * 1.05)
				{
					return promptList[0];
				}
			}
			else if (connector.Shape == ConnectorSharp.Rect && fiConn.Shape == ConnectorSharp.Rect)
			{
				double num6 = (connector.Width - fiConn.Height) / 2.0;
				double num7 = (connector.Height - fiConn.Height) / 2.0;
				if (beDown)
				{
					if (num > num6 * 1.05)
					{
						return promptList[0];
					}
				}
				else if (num2 > num7 * 1.05)
				{
					return promptList[0];
				}
			}
			return "";
		}

		public bool JudgeFamilyInstanceConn(YJKPiping<Duct> hyDuct, Connector fiConn, ref int connType)
		{
			connType = -1;
			XYZ xyz = hyDuct.StartConnector.Origin.Subtract(hyDuct.EndConnector.Origin).Normalize();
			XYZ basisZ = fiConn.CoordinateSystem.BasisZ;
			double num = 0.0001;
			if (basisZ.IsAlmostEqualTo(xyz, num) || basisZ.IsAlmostEqualTo(-1.0 * xyz, num))
			{
				connType = 11;
				return true;
			}
			XYZ xyz2 = new XYZ(0.0, 0.0, 1.0);
			if (basisZ.IsAlmostEqualTo(xyz2, num) && fiConn.Origin.Z < hyDuct.StartConnector.Origin.Z)
			{
				connType = 33;
				return true;
			}
			XYZ xyz3 = basisZ.CrossProduct(xyz).Normalize();
			if (xyz3.IsAlmostEqualTo(xyz2, num) || xyz3.IsAlmostEqualTo(-1.0 * xyz2, num))
			{
				connType = 22;
				return true;
			}
			return false;
		}

		public FamilyInstance NewElbowFitting(IList<Duct> ducts, IList<XYZ> xyzs, FamilySymbol fs)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance familyInstance2 = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count < 2 || xyzs.Count < 2)
				{
					throw new System.ArgumentNullException("Duct or XYZ", "is null");
				}
				document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
				UnifiedModified.ActivateFamilySymbol(fs);
				familyInstance2 = document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				Connector hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance2);
				if (hypiping.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping2.StartConnector.Connector.Shape != hyelementConnector.Shape)
				{
					throw new Exception("管件和风管的截面类型不匹配");
				}
				hypiping.ChangeFirstElbow(fs);
				hypiping2.ChangeFirstElbow(fs);
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, true);
				if (xyz != null)
				{
					Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
					Connector closeConnector2 = hypiping2.GetCloseConnector(xyz, false);
					if (closeConnector == null || closeConnector2 == null || closeConnector.IsConnected || closeConnector2.IsConnected)
					{
						throw new Exception("风管已经连接");
					}
					familyInstance = document.Create.NewElbowFitting(closeConnector, closeConnector2);
				}
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance2 != null)
				{
					document.DeleteElement(familyInstance2);
				}
			}
			return result;
		}

		public FamilyInstance NewElbowFitting(IList<Duct> ducts, IList<XYZ> xyzs, FamilySymbol fs, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 2 || xyzs.Count != 2)
				{
					connMSG = "请选择两条风管进行弯头连接！";
					result = familyInstance;
				}
				else
				{
					Autodesk.Revit.DB.Document document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					if (hypiping.Piping.UniqueId.Equals(hypiping2.Piping.UniqueId))
					{
						connMSG = "同一根风管不能构成弯头连接！";
						result = familyInstance;
					}
					else if (hypiping.IsParallel(hypiping2.Piping))
					{
						connMSG = "两平行风管不能构成弯头连接！";
						result = familyInstance;
					}
					else if (fs == null)
					{
						this.NewSomeConnect(ducts);
						result = null;
					}
					else
					{
						UnifiedModified.ActivateFamilySymbol(fs);
						FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
						document.Regenerate();
						Connector hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance2);
						if (hypiping.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping2.StartConnector.Connector.Shape != hyelementConnector.Shape)
						{
							connMSG = "管件和风管的截面类型不匹配!";
							if (familyInstance2 != null)
							{
								document.DeleteElement(familyInstance2);
							}
							result = familyInstance;
						}
						else
						{
							if (familyInstance2 != null)
							{
								document.DeleteElement(familyInstance2);
							}
							bool flag = hypiping.ChangeFirstElbow(fs);
							bool flag2 = hypiping2.ChangeFirstElbow(fs);
							if (!flag || !flag2)
							{
								connMSG = "设置风管弯头，\n首选连接件出错！";
								result = familyInstance;
							}
							else
							{
								XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, true);
								if (xyz == null)
								{
									connMSG = "两风管没有交点，\n不能进行弯头连接！";
									result = familyInstance;
								}
								else
								{
									Connector nearConnector = this.GetNearConnector(xyz, xyzs[0], hypiping);
									Connector nearConnector2 = this.GetNearConnector(xyz, xyzs[1], hypiping2);
									if (nearConnector == null || nearConnector2 == null)
									{
										connMSG = "找不到连接口，\n无法完成连接！";
										result = familyInstance;
									}
									else if (nearConnector.IsConnected || nearConnector2.IsConnected)
									{
										connMSG = "风管已经连接！";
										result = familyInstance;
									}
									else
									{
										double num = nearConnector.CoordinateSystem.BasisZ.AngleTo(nearConnector2.CoordinateSystem.BasisZ) * 180.0 / Math.PI;
										if (num < 89.5 || num >= 178.0)
										{
											connMSG = "两风管的夹角应在90°-180°之间\n(包含90°，但不包含180°)！";
											result = familyInstance;
										}
										else
										{
											familyInstance = document.Create.NewElbowFitting(nearConnector, nearConnector2);
											result = familyInstance;
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				YJKMessageBox.Show("两风管的位置关系不对,无法进行弯头连接");
				result = null;
			}
			return result;
		}

		private XYZ SetZValue(XYZ point, double value)
		{
			return new XYZ(point.X, point.Y, value);
		}

		private Connector GetNearConnector(XYZ intersectionPoint, XYZ selectPoint, YJKPiping<Duct> myCustomDuct)
		{
			XYZ xyz = this.SetZValue(myCustomDuct.StartConnector.Origin, 0.0);
			XYZ xyz2 = this.SetZValue(myCustomDuct.EndConnector.Origin, 0.0);
			XYZ xyz3 = this.SetZValue(intersectionPoint, 0.0);
			YJKLine hyline = new YJKLine(Line.CreateBound(xyz, xyz2));
			XYZ project = hyline.GetProject(selectPoint);
			XYZ xyz4 = (this.SetZValue(project, 0.0) - this.SetZValue(intersectionPoint, 0.0)).Normalize();
			bool flag = hyline.IsPointOnLine(this.SetZValue(intersectionPoint, 0.0));
			XYZ xyz5 = (xyz - xyz3).Normalize();
			(xyz2 - xyz3).Normalize();
			Connector connector = myCustomDuct.StartConnector.Connector;
			Connector connector2 = myCustomDuct.EndConnector.Connector;
			if (!flag)
			{
				return myCustomDuct.GetCloseConnector(intersectionPoint, false);
			}
			if (xyz5.DistanceTo(xyz4) < 0.001)
			{
				return myCustomDuct.EndConnector.Connector;
			}
			return myCustomDuct.StartConnector.Connector;
		}

		private bool BeMiddlePointDuct(YJKPiping<Duct> hyDuct)
		{
			XYZ globalPoint = hyDuct.GlobalPoint;
			if (globalPoint == null)
			{
				return true;
			}
			double num = hyDuct.StartConnector.Origin.DistanceTo(globalPoint);
			double num2 = hyDuct.EndConnector.Origin.DistanceTo(globalPoint);
			return Math.Abs(num - num2) < 0.00984251968503937;
		}

		public void NewDuctAccessory(List<YJKPiping<Duct>> lldu, XYZ intersectionPoint, ICollection<ElementId> fi, Autodesk.Revit.DB.Document doc)
		{
			if (lldu.Count < 2)
			{
				throw new System.ArgumentNullException("Duct is null");
			}
			YJKPiping<Duct> hypiping = lldu[0];
			YJKPiping<Duct> hypiping2 = lldu[1];
			new ConnectHelper();
			Connector closeConnector = hypiping.GetCloseConnector(intersectionPoint, false);
			Connector closeConnector2 = hypiping2.GetCloseConnector(intersectionPoint, false);
			IList<Connector> list = new List<Connector>();
			if (fi.Count > 0)
			{
				foreach (ElementId elementId in fi)
				{
					ConnectorSetIterator connectorSetIterator = (doc.GetElementById(elementId) as FamilyInstance).MEPModel.ConnectorManager.Connectors.ForwardIterator();
					int num = 0;
					while (connectorSetIterator.MoveNext())
					{
						num++;
						Connector connector = connectorSetIterator.Current as Connector;
						if (connector != null && !connector.IsConnected && !list.Contains(connector) && connector.Domain == (Autodesk.Revit.DB.Domain)1)
						{
							list.Add(connector);
						}
						if (num > 10)
						{
							break;
						}
					}
				}
			}
			Connector connector2 = this.getthenearstConnector(closeConnector, list);
			Connector connector3 = this.getthenearstConnector(closeConnector2, list);
			XYZ xyz = (closeConnector.Origin - closeConnector2.Origin).Normalize();
			closeConnector.ConnectTo(connector2);
			doc.Regenerate();
			closeConnector2.ConnectTo(connector3);
			doc.Regenerate();
			ElementTransformUtils.MoveElement(doc, hypiping.Piping.Id, xyz * 1E-06);
			ElementTransformUtils.MoveElement(doc, hypiping.Piping.Id, -1.0 * xyz * 1E-06);
			ElementTransformUtils.MoveElement(doc, hypiping2.Piping.Id, xyz * 1E-06);
			ElementTransformUtils.MoveElement(doc, hypiping2.Piping.Id, -1.0 * xyz * 1E-06);
			doc.Regenerate();
		}

		public Connector getthenearstConnector(Connector conn, IList<Connector> list)
		{
			List<double> list2 = new List<double>();
			foreach (Connector connector in list)
			{
				list2.Add(conn.Origin.DistanceTo(connector.Origin));
			}
			int index = list2.FindIndex((double a) => a == list2.Min());
			return list.ElementAt(index);
		}

		public bool NewTransitionFitting(IList<Duct> ducts)
		{
			bool flag = false;
			bool result;
			try
			{
				if (ducts.Count < 2)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				new YJKPiping<Duct>(ducts[1]);
				IList<Connector> list = new List<Connector>();
				list = hypiping.GetAppropriateConnector(ducts[1]);
				if (list.Count > 1 && list[0] != null && list[1] != null && !list[0].IsConnected && !list[1].IsConnected)
				{
					document.Create.NewTransitionFitting(list[0], list[1]);
					flag = true;
				}
				document.Regenerate();
				result = flag;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败！");
			}
			return result;
		}

		public FamilyInstance NewTransitionFitting(IList<Duct> ducts, bool resultFlag, FamilySymbol fs)
		{
			FamilyInstance result;
			try
			{
				if (ducts.Count < 2)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
				if (hypiping.Shape == hypiping2.Shape)
				{
					hypiping.ChangeFirstTransition(fs);
					hypiping2.ChangeFirstTransition(fs);
				}
				else
				{
					hypiping.ChangeFirstTYDF(fs);
					hypiping2.ChangeFirstTYDF(fs);
				}
				document.Regenerate();
				IList<Connector> list = new List<Connector>();
				list = hypiping.GetAppropriateConnector(ducts[1]);
				if (list.Count <= 1)
				{
					throw new Exception("获取连接器失败");
				}
				if (list[0] == null || list[1] == null || list[0].IsConnected || list[1].IsConnected)
				{
					throw new Exception("风管已经连接");
				}
				FamilyInstance familyInstance = document.Create.NewTransitionFitting(list[0], list[1]);
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FamilyInstance NewTransitionFitting(IList<Duct> ducts, bool resultFlag, FamilySymbol fs, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 2)
				{
					connMSG = "请选择两根风管，否则无法构成正确的连接关系！";
					result = familyInstance;
				}
				else
				{
					Autodesk.Revit.DB.Document document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
					if (hypiping.Piping.UniqueId.Equals(hypiping2.Piping.UniqueId))
					{
						connMSG = "同一根风管不能构成连接关系！";
						result = familyInstance;
					}
					else if (fs == null)
					{
						this.NewSomeConnect(ducts);
						result = null;
					}
					else
					{
						bool flag;
						bool flag2;
						if (hypiping.Shape == hypiping2.Shape)
						{
							flag = hypiping.ChangeFirstTransition(fs);
							flag2 = hypiping2.ChangeFirstTransition(fs);
						}
						else
						{
							flag = hypiping.ChangeFirstTYDF(fs);
							flag2 = hypiping2.ChangeFirstTYDF(fs);
						}
						if (!flag || !flag2)
						{
							connMSG = "设置连接件首选项失败！";
							result = familyInstance;
						}
						else
						{
							document.Regenerate();
							if (!hypiping.IsXYParallel(ducts[1]))
							{
								connMSG = "两风管必须在同一直线上或者平行！";
								result = null;
							}
							else
							{
								IList<Connector> list = new List<Connector>();
								list = hypiping.GetAppropriateConnector(ducts[1]);
								if (list.Count != 2 || list[0] == null || list[1] == null)
								{
									connMSG = "获取连接器失败！";
									result = null;
								}
								else if (list[0].IsConnected || list[1].IsConnected)
								{
									connMSG = "风管已经连接！";
									result = null;
								}
								else
								{
									familyInstance = document.Create.NewTransitionFitting(list[0], list[1]);
									result = familyInstance;
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool NewLHWFitting(IList<Duct> ducts)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 2)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<Connector> appropriateConnector = hypiping.GetAppropriateConnector(hypiping2.Piping);
				if (appropriateConnector == null || appropriateConnector.Count <= 1)
				{
					throw new Exception("请检查风管的位置关系能否构成来回弯");
				}
				XYZ point = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
				XYZ lineProject = hypiping.GetLineProject(point);
				XYZ lineProject2 = hypiping2.GetLineProject(point);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(YJKRevitTools.NewDuct(hypiping.Piping, lineProject, lineProject2));
				IList<Duct> list = new List<Duct>();
				list.Add(hypiping.Piping);
				list.Add(hypiping3.Piping);
				this.NewElbowFitting(list);
				list.Clear();
				list.Add(hypiping2.Piping);
				list.Add(hypiping3.Piping);
				this.NewElbowFitting(list);
				document.Regenerate();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public IList<FamilyInstance> NewLHWFitting(IList<Duct> ducts, bool resultFlag, FamilySymbol fs)
		{
			IList<FamilyInstance> list = new List<FamilyInstance>();
			Autodesk.Revit.DB.Document document = null;
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			IList<FamilyInstance> result;
			try
			{
				if (ducts.Count < 2)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
				UnifiedModified.ActivateFamilySymbol(fs);
				familyInstance = document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				Connector hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance);
				if (hypiping.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping2.StartConnector.Connector.Shape != hyelementConnector.Shape)
				{
					throw new Exception("管件和风管的截面类型不匹配");
				}
				hypiping.ChangeFirstElbow(fs);
				hypiping2.ChangeFirstElbow(fs);
				document.Regenerate();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<Connector> appropriateConnector = hypiping.GetAppropriateConnector(hypiping2.Piping);
				if (appropriateConnector == null || appropriateConnector.Count <= 1)
				{
					throw new Exception("请检查风管的位置关系能否构成来回弯");
				}
				XYZ point = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
				XYZ lineProject = hypiping.GetLineProject(point);
				XYZ lineProject2 = hypiping2.GetLineProject(point);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(YJKRevitTools.NewDuct(hypiping.Piping, lineProject, lineProject2));
				hypiping3.ChangeFirstElbow(fs);
				IList<Duct> list2 = new List<Duct>();
				list2.Add(hypiping.Piping);
				list2.Add(hypiping3.Piping);
				this.NewElbowFitting(list2);
				list2.Clear();
				list2.Add(hypiping2.Piping);
				list2.Add(hypiping3.Piping);
				this.NewElbowFitting(list2);
				document.Regenerate();
				subTransaction.Commit();
				result = list;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public IList<FamilyInstance> NewLHWFitting(IList<Duct> ducts, bool resultFlag, FamilySymbol fs, ref string connMSG)
		{
			IList<FamilyInstance> list = new List<FamilyInstance>();
			SubTransaction subTransaction = null;
			IList<FamilyInstance> result;
			try
			{
				if (ducts.Count != 2)
				{
					connMSG = "来回弯连接必须是两根风管！";
					result = null;
				}
				else
				{
					Autodesk.Revit.DB.Document document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
					if (hypiping.Piping.UniqueId.Equals(hypiping2.Piping.UniqueId))
					{
						connMSG = "两根风管不能为同一风管！";
						result = null;
					}
					else if (hypiping2.StartConnector.Connector.Shape != hypiping.StartConnector.Connector.Shape)
					{
						connMSG = "两根风管截面类型必须相同！";
						result = null;
					}
					else if (hypiping.IsPipingOnLine(hypiping2.Piping))
					{
						connMSG = "两根风管不能在同一直线上！";
						result = null;
					}
					else if (!hypiping.IsParallel(hypiping2.Piping))
					{
						connMSG = "两根风管必须平行！";
						result = null;
					}
					else
					{
						UnifiedModified.ActivateFamilySymbol(fs);
						FamilyInstance familyInstance = document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
						document.Regenerate();
						Connector hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance);
						if (hypiping.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping2.StartConnector.Connector.Shape != hyelementConnector.Shape)
						{
							connMSG = "管件和风管的截面类型不匹配！";
							if (familyInstance != null)
							{
								document.DeleteElement(familyInstance);
							}
							result = null;
						}
						else
						{
							if (familyInstance != null)
							{
								document.DeleteElement(familyInstance);
							}
							bool flag = hypiping.ChangeFirstElbow(fs);
							bool flag2 = hypiping2.ChangeFirstElbow(fs);
							if (!flag || !flag2)
							{
								connMSG = "设置风管弯头首选连接件出错！";
								result = null;
							}
							else
							{
								document.Regenerate();
								subTransaction = new SubTransaction(document);
								subTransaction.Start();
								IList<Connector> list2 = new List<Connector>();
								list2 = hypiping.GetAppropriateConnector(hypiping2.Piping);
								Connector connector = list2[0];
								Connector connector2;
								if (list2[0].Origin.IsAlmostEqualTo(hypiping.StartConnector.Origin))
								{
									connector2 = hypiping.EndConnector.Connector;
								}
								else
								{
									connector2 = hypiping.StartConnector.Connector;
								}
								Connector connector3 = list2[1];
								Connector connector4;
								if (list2[1].Origin.IsAlmostEqualTo(hypiping2.StartConnector.Origin))
								{
									connector4 = hypiping2.EndConnector.Connector;
								}
								else
								{
									connector4 = hypiping2.StartConnector.Connector;
								}
								XYZ xyz = connector.Origin.Subtract(connector2.Origin).Normalize();
								XYZ xyz2 = connector4.Origin.Subtract(connector3.Origin).Normalize();
								if (!xyz.IsAlmostEqualTo(xyz2, 0.001))
								{
									connMSG = "两水管相近的两个接口无法进行连接,\n请检查管道的位置关系！";
									result = null;
								}
								else
								{
									XYZ basisZ = connector.CoordinateSystem.BasisZ;
									XYZ xyz3 = connector3.Origin.Subtract(connector.Origin).CrossProduct(basisZ).Normalize();
									XYZ xyz4 = basisZ.CrossProduct(xyz3).Normalize();
									XYZ xyz5 = basisZ.Add(xyz4).Normalize();
									Line otherLine = Line.CreateBound(connector.Origin, connector.Origin + 3.0 * xyz5);
									XYZ xyz6 = new YJKLine(Line.CreateBound(connector3.Origin, connector4.Origin)).IntersectionPoint(otherLine, 0.0003);
									if (xyz6 == null)
									{
										connMSG = "水管位置不满足连接条件，\n请检查管道的位置关系!";
										result = null;
									}
									else if (connector.Origin.DistanceTo(xyz6) > connector.Origin.DistanceTo(connector4.Origin))
									{
										connMSG = "水管位置不满足连接条件!";
										result = null;
									}
									else
									{
										double diameter = hypiping.GetDiameter();
										double diameter2 = hypiping2.GetDiameter();
										double distance = hypiping.GetDistance(hypiping2.Piping);
										double num = (diameter <= diameter2) ? diameter2 : diameter;
										if (distance <= 1.5 * num)
										{
											if (connector == null || connector.IsConnected || connector3 == null || connector3.IsConnected)
											{
												connMSG = "水管已经连接";
												return null;
											}
											ConnectDuctManage connectDuctManage = new ConnectDuctManage();
											ConnectDuctStyle connectDuctStyle = ConnectDuctStyle.Empty;
											int ductShape = connectDuctManage.GetDuctShape(hypiping.Piping);
											if (ductShape == 1)
											{
												connectDuctStyle = ConnectDuctStyle.Variable;
											}
											if (ductShape == 0)
											{
												connectDuctStyle = ConnectDuctStyle.VariableRound;
											}
											if (connectDuctStyle == ConnectDuctStyle.Empty)
											{
												connMSG = "仅支持矩形与圆形风管连接！";
												return null;
											}
											List<string> fittingNames = connectDuctManage.GetFittingNames(connectDuctStyle);
											FamilySymbol familySymbol = connectDuctManage.GetFamilySymbol(fittingNames[0], document, ref connMSG);
											if (!string.IsNullOrEmpty(connMSG))
											{
												return null;
											}
											hypiping.ChangeFirstTransition(familySymbol);
											document.Regenerate();
											list.Add(document.Create.NewTransitionFitting(connector, connector3));
										}
										else
										{
											YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(YJKRevitTools.NewDuct((diameter <= diameter2) ? hypiping.Piping : hypiping2.Piping, connector.Origin, xyz6));
											Connector connector5;
											Connector connector6;
											Connector connector7;
											Connector connector8;
											if (list2[0].Origin.IsAlmostEqualTo(hypiping3.StartConnector.Origin))
											{
												connector5 = connector;
												connector6 = hypiping3.StartConnector.Connector;
												connector7 = connector3;
												connector8 = hypiping3.EndConnector.Connector;
											}
											else
											{
												connector5 = connector;
												connector6 = hypiping3.EndConnector.Connector;
												connector7 = connector3;
												connector8 = hypiping3.StartConnector.Connector;
											}
											if (connector5 == null || connector5.IsConnected || connector6 == null || connector6.IsConnected || connector7 == null || connector7.IsConnected || connector8 == null || connector8.IsConnected)
											{
												connMSG = "水管已经连接";
												return null;
											}
											list.Add(document.Create.NewElbowFitting(connector5, connector6));
											list.Add(document.Create.NewElbowFitting(connector7, connector8));
										}
										document.Regenerate();
										subTransaction.Commit();
										result = list;
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				YJKMessageBox.Show("风管的位置关系不对,无法进行来回弯连接");
				result = null;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public IList<FamilyInstance> NewLHWFitting(IList<Duct> ducts, bool resultFlag, FamilySymbol elbowFS, FamilySymbol reduceFS, ref string connMSG)
		{
			IList<FamilyInstance> list = new List<FamilyInstance>();
			SubTransaction subTransaction = null;
			IList<FamilyInstance> result;
			try
			{
				if (ducts.Count != 2)
				{
					connMSG = "来回弯连接必须是两根风管！";
					result = null;
				}
				else
				{
					Autodesk.Revit.DB.Document document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
					if (hypiping.Piping.UniqueId.Equals(hypiping2.Piping.UniqueId))
					{
						connMSG = "两根风管不能为同一风管！";
						result = null;
					}
					else if (hypiping2.StartConnector.Connector.Shape != hypiping.StartConnector.Connector.Shape)
					{
						connMSG = "两根风管截面类型必须相同！";
						result = null;
					}
					else
					{
						UnifiedModified.ActivateFamilySymbol(elbowFS);
						FamilyInstance familyInstance = document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), elbowFS, (Autodesk.Revit.DB.Structure.StructuralType)0);
						document.Regenerate();
						Connector hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance);
						if (hypiping.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping2.StartConnector.Connector.Shape != hyelementConnector.Shape)
						{
							connMSG = "管件和风管的截面类型不匹配！";
							if (familyInstance != null)
							{
								document.DeleteElement(familyInstance);
							}
							result = null;
						}
						else
						{
							if (familyInstance != null)
							{
								document.DeleteElement(familyInstance);
							}
							bool flag = hypiping.ChangeFirstElbow(elbowFS);
							bool flag2 = hypiping2.ChangeFirstElbow(elbowFS);
							if (!flag || !flag2)
							{
								connMSG = "设置风管弯头首选连接件出错！";
								result = null;
							}
							else
							{
								document.Regenerate();
								subTransaction = new SubTransaction(document);
								subTransaction.Start();
								IList<Connector> appropriateConnector = hypiping.GetAppropriateConnector(hypiping2.Piping);
								if (appropriateConnector == null || appropriateConnector.Count != 2)
								{
									connMSG = "两风管位置不满足来回弯连接条件！";
									result = null;
								}
								else
								{
									hypiping.GetDistance(hypiping2.Piping);
									XYZ point = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
									XYZ lineProject = hypiping.GetLineProject(point);
									XYZ lineProject2 = hypiping2.GetLineProject(point);
									if (lineProject == null || lineProject2 == null)
									{
										connMSG = "确定两风管连接口失败！";
										result = null;
									}
									else
									{
										YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(YJKRevitTools.NewDuct((hypiping.GetDiameter() <= hypiping2.GetDiameter()) ? hypiping.Piping : hypiping2.Piping, lineProject, lineProject2));
										if (!hypiping3.ChangeFirstElbow(elbowFS))
										{
											connMSG = "设置风管弯头首选连接件出错！";
											result = null;
										}
										else
										{
											IList<Duct> list2 = new List<Duct>();
											list2.Add(hypiping.Piping);
											list2.Add(hypiping3.Piping);
											if (!this.NewElbowFitting(list2, ref connMSG))
											{
												result = null;
											}
											else
											{
												list2.Clear();
												list2.Add(hypiping2.Piping);
												list2.Add(hypiping3.Piping);
												if (!this.NewElbowFitting(list2, ref connMSG))
												{
													result = null;
												}
												else
												{
													document.Regenerate();
													subTransaction.Commit();
													result = list;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool NewTeeFitting(IList<Duct> ducts)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 3)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				new List<Duct>();
				IList<Duct> list = this.OrderDuctPosition(ducts);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list[1]);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list[2]);
				YJKPiping<Duct> hypiping4 = null;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
				if (xyz != null)
				{
					if (hypiping2.Piping.UniqueId == hypiping3.Piping.UniqueId)
					{
						new List<Duct>();
						hypiping4 = new YJKPiping<Duct>(((IList<Duct>)hypiping2.Separate(xyz))[1]);
					}
					else if (hypiping2.IsPipingOnLine(hypiping3.Piping))
					{
						hypiping4 = new YJKPiping<Duct>(hypiping3.Piping);
					}
					if (hypiping4 != null)
					{
						Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
						Connector appropriateConnector2 = hypiping2.GetAppropriateConnector(xyz);
						Connector appropriateConnector3 = hypiping4.GetAppropriateConnector(xyz);
						if (appropriateConnector != null && !appropriateConnector.IsConnected)
						{
							document.Create.NewTeeFitting(appropriateConnector2, appropriateConnector3, appropriateConnector);
							flag = true;
						}
					}
				}
				document.Regenerate();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败，可能由于管道角度过大或者过小！");
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		private IList<Duct> OrderDuctPosition(IList<Duct> ducts)
		{
			IList<Duct> list = new List<Duct>();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2]);
			if (3 == ducts.Count)
			{
				if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsParallel(hypiping2.Piping))
				{
					list.Add(hypiping3.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsParallel(hypiping3.Piping))
				{
					list.Add(hypiping2.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
				}
				else
				{
					list = ducts;
				}
			}
			else if (4 == ducts.Count)
			{
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[3]);
				if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsParallel(hypiping2.Piping))
				{
					list = ducts;
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsParallel(hypiping3.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping4.Piping);
				}
				else if (hypiping.IsPipingOnLine(hypiping4.Piping) || hypiping.IsParallel(hypiping4.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping4.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping3.Piping);
				}
				else
				{
					list = ducts;
				}
			}
			return list;
		}

		private IList<Duct> OrderDuctPosition(IList<Duct> ducts, ref IList<XYZ> xyzs)
		{
			IList<Duct> list = new List<Duct>();
			IList<XYZ> list2 = new List<XYZ>();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
			if (3 == ducts.Count)
			{
				if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsParallel(hypiping2.Piping))
				{
					list.Add(hypiping3.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
					list2.Add(hypiping3.GlobalPoint);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping2.GlobalPoint);
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsPipingOnLine(hypiping3.Piping))
				{
					list.Add(hypiping2.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
					list2.Add(hypiping2.GlobalPoint);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping3.GlobalPoint);
				}
				else
				{
					list = ducts;
					list2 = xyzs;
				}
			}
			else if (4 == ducts.Count)
			{
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[3]);
				if (hypiping.IsPipingOnLine(hypiping2.Piping))
				{
					list = ducts;
					list2 = xyzs;
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping4.Piping);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping3.GlobalPoint);
					list2.Add(hypiping2.GlobalPoint);
					list2.Add(hypiping4.GlobalPoint);
				}
				else if (hypiping.IsPipingOnLine(hypiping4.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping4.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping3.Piping);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping4.GlobalPoint);
					list2.Add(hypiping2.GlobalPoint);
					list2.Add(hypiping3.GlobalPoint);
				}
				else
				{
					list = ducts;
					list2 = xyzs;
				}
			}
			xyzs = list2;
			return list;
		}

		public FamilyInstance NewTeeFitting(IList<Duct> ducts, IList<XYZ> xyzs, FamilySymbol fs)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance2 = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count < 3 || xyzs.Count < 3)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				document = ducts[0].Document;
				new List<Duct>();
				IList<Duct> list = this.OrderDuctPosition(ducts, ref xyzs);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list[0], xyzs[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list[1], xyzs[1]);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list[2], xyzs[2]);
				YJKPiping<Duct> hypiping4 = null;
				UnifiedModified.ActivateFamilySymbol(fs);
				familyInstance2 = document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				Connector hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance2);
				if (hypiping.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping2.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping3.StartConnector.Connector.Shape != hyelementConnector.Shape)
				{
					throw new Exception("管件和风管的截面类型不匹配");
				}
				hypiping.ChangeFirstTee(fs);
				hypiping2.ChangeFirstTee(fs);
				hypiping3.ChangeFirstTee(fs);
				document.Regenerate();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, true);
				if (xyz != null)
				{
					if (hypiping2.Piping.UniqueId == hypiping3.Piping.UniqueId)
					{
						new List<Duct>();
						hypiping4 = new YJKPiping<Duct>(((IList<Duct>)hypiping2.Separate(xyz))[1], hypiping3.GlobalPoint);
					}
					else if (hypiping2.IsPipingOnLine(hypiping3.Piping) || hypiping2.IsParallel(hypiping3.Piping))
					{
						hypiping4 = new YJKPiping<Duct>(hypiping3.Piping, hypiping3.GlobalPoint);
					}
					if (hypiping4 == null)
					{
						throw new Exception("请检查风管位置是否正确");
					}
					Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
					Connector closeConnector2 = hypiping2.GetCloseConnector(xyz, false);
					Connector closeConnector3 = hypiping4.GetCloseConnector(xyz, false);
					if (closeConnector == null || closeConnector.IsConnected || closeConnector2 == null || closeConnector2.IsConnected || closeConnector3 == null || closeConnector3.IsConnected)
					{
						throw new Exception("风管已经连接");
					}
					familyInstance = document.Create.NewTeeFitting(closeConnector2, closeConnector3, closeConnector);
				}
				subTransaction.Commit();
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance2 != null)
				{
					document.DeleteElement(familyInstance2);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewTeeFittingY(IList<Duct> ducts, IList<XYZ> xyzs, FamilySymbol fs)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count < 3 || xyzs.Count < 3)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
				ConnectHelper connectHelper = new ConnectHelper();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 1);
				if (xyz == null)
				{
					throw new Exception("请检查风管位置是否正确");
				}
				YJKPiping<Duct> hypiping4;
				if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
				{
					new List<Duct>();
					hypiping4 = new YJKPiping<Duct>(((IList<Duct>)hypiping.Separate(xyz))[1], hypiping2.GlobalPoint);
					document.Regenerate();
				}
				else
				{
					hypiping4 = new YJKPiping<Duct>(hypiping2.Piping, hypiping2.GlobalPoint);
				}
				UnifiedModified.ActivateFamilySymbol(fs);
				FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(xyz, fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				if (familyInstance2.GetParameter("风管直径1") == null && familyInstance2.GetParameter("风管宽度1") == null && familyInstance2.GetParameter("风管高度1") == null)
				{
					subTransaction.RollBack();
					subTransaction = null;
					result = this.NewTeeFitting(ducts, xyzs, fs);
				}
				else
				{
					Parameter parameter = hypiping.Piping.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
					Parameter parameter2 = hypiping.Piping.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
					Parameter parameter3 = hypiping.Piping.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM);
					if (parameter3 != null)
					{
						if (!parameter3.IsReadOnly)
						{
							familyInstance2.GetParameter("风管直径1").Set(parameter3.AsDouble());
						}
					}
					else
					{
						if (!parameter.IsReadOnly)
						{
							familyInstance2.GetParameter("风管宽度1").Set(parameter.AsDouble());
						}
						if (!parameter2.IsReadOnly)
						{
							familyInstance2.GetParameter("风管高度1").Set(parameter2.AsDouble());
						}
					}
					this.SettingTeeParameter(familyInstance2, hypiping, hypiping3, 2);
					this.SettingTeeParameter(familyInstance2, hypiping, hypiping4, 1);
					document.Regenerate();
					XYZ point = (familyInstance2.Location as LocationPoint).Point;
					Connector closeConnector = hypiping.GetCloseConnector(point, false);
					Connector connector = connectHelper.GetTeeMainConnector(familyInstance2);
					double num = 0.0;
					Line line = YJKLineEx.YJKGetBound(xyz, xyz + new XYZ(0.0, 0.0, 1.0));
					line.MakeUnbound();
					if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
					{
						num = closeConnector.CoordinateSystem.BasisZ.AngleTo(-connector.CoordinateSystem.BasisZ);
					}
					if (num > 0.0001)
					{
						ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, num);
						document.Regenerate();
						if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisZ))
						{
							ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, Math.PI*2 - 2.0 * num);
							document.Regenerate();
						}
					}
					Connector closeConnector2 = hypiping3.GetCloseConnector(point, false);
					Connector distanceConnector = hypiping3.GetDistanceConnector(point);
					Connector nearConnector = connectHelper.GetNearConnector(familyInstance2, distanceConnector);
					if (!this.IsSameConnector(closeConnector2, nearConnector))
					{
						Line line2 = YJKLineEx.YJKGetBound(xyz, connector.Origin);
						line2.MakeUnbound();
						ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, Math.PI);
					}
					document.Regenerate();
					Connector distanceConnector2 = hypiping.GetDistanceConnector(point);
					connector = connectHelper.GetNearConnector(familyInstance2, distanceConnector2);
					if (!this.IsSameConnector(closeConnector, connector))
					{
						throw new Exception("入口接头类型不匹配");
					}
					Connector closeConnector3 = hypiping4.GetCloseConnector(point, false);
					Connector distanceConnector3 = hypiping4.GetDistanceConnector(point);
					Connector nearConnector2 = connectHelper.GetNearConnector(familyInstance2, distanceConnector3);
					if (!this.IsSameConnector(closeConnector3, nearConnector2))
					{
						throw new Exception("出口接头类型不匹配");
					}
					distanceConnector = hypiping3.GetDistanceConnector(point);
					nearConnector = connectHelper.GetNearConnector(familyInstance2, distanceConnector);
					if (!this.IsSameConnector(closeConnector2, nearConnector))
					{
						throw new Exception("支管接头类型不匹配");
					}
					hypiping.Extend(connector);
					hypiping3.Extend(nearConnector);
					hypiping4.Extend(nearConnector2);
					document.Regenerate();
					FamilyInstance familyInstance3 = familyInstance2;
					subTransaction.Commit();
					result = familyInstance3;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewTeeFittingY(IList<Duct> ducts, IList<XYZ> xyzs, FamilySymbol fs, ref string connMSG)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 3 || xyzs.Count != 3)
				{
					connMSG = "三通连接必须是三条风管！";
					result = familyInstance;
				}
				else
				{
					document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					ConnectHelper connectHelper = new ConnectHelper();
					subTransaction = new SubTransaction(document);
					subTransaction.Start();
					XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, 1);
					if (xyz == null)
					{
						throw new Exception("请检查风管位置是否正确");
					}
					YJKPiping<Duct> hypiping4;
					if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
					{
						new List<Duct>();
						hypiping4 = new YJKPiping<Duct>(((IList<Duct>)hypiping.Separate(xyz))[1], hypiping2.GlobalPoint);
						document.Regenerate();
					}
					else
					{
						hypiping4 = new YJKPiping<Duct>(hypiping2.Piping, hypiping2.GlobalPoint);
					}
					UnifiedModified.ActivateFamilySymbol(fs);
					FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(xyz, fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
					document.Regenerate();
					if (familyInstance2.GetParameter("风管直径1") == null && familyInstance2.GetParameter("风管宽度1") == null && familyInstance2.GetParameter("风管高度1") == null)
					{
						subTransaction.RollBack();
						subTransaction = null;
						result = this.NewTeeFitting(ducts, xyzs, fs);
					}
					else
					{
						Parameter parameter = hypiping.Piping.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
						Parameter parameter2 = hypiping.Piping.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
						Parameter parameter3 = hypiping.Piping.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM);
						if (parameter3 != null)
						{
							if (!parameter3.IsReadOnly)
							{
								familyInstance2.GetParameter("风管直径1").Set(parameter3.AsDouble());
							}
						}
						else
						{
							if (!parameter.IsReadOnly)
							{
								familyInstance2.GetParameter("风管宽度1").Set(parameter.AsDouble());
							}
							if (!parameter2.IsReadOnly)
							{
								familyInstance2.GetParameter("风管高度1").Set(parameter2.AsDouble());
							}
						}
						this.SettingTeeParameter(familyInstance2, hypiping, hypiping3, 2);
						this.SettingTeeParameter(familyInstance2, hypiping, hypiping4, 1);
						document.Regenerate();
						XYZ point = (familyInstance2.Location as LocationPoint).Point;
						Connector closeConnector = hypiping.GetCloseConnector(point, false);
						Connector connector = connectHelper.GetTeeMainConnector(familyInstance2);
						double num = 0.0;
						Line line = YJKLineEx.YJKGetBound(xyz, xyz + new XYZ(0.0, 0.0, 1.0));
						line.MakeUnbound();
						if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
						{
							num = closeConnector.CoordinateSystem.BasisZ.AngleTo(-connector.CoordinateSystem.BasisZ);
						}
						if (num > 0.0001)
						{
							ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, num);
							document.Regenerate();
							if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisZ))
							{
								ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, Math.PI*2 - 2.0 * num);
								document.Regenerate();
							}
						}
						Connector closeConnector2 = hypiping3.GetCloseConnector(point, false);
						Connector distanceConnector = hypiping3.GetDistanceConnector(point);
						Connector nearConnector = connectHelper.GetNearConnector(familyInstance2, distanceConnector);
						if (!this.IsSameConnector(closeConnector2, nearConnector))
						{
							Line line2 = YJKLineEx.YJKGetBound(xyz, connector.Origin);
							line2.MakeUnbound();
							ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, Math.PI);
						}
						document.Regenerate();
						Connector distanceConnector2 = hypiping.GetDistanceConnector(point);
						connector = connectHelper.GetNearConnector(familyInstance2, distanceConnector2);
						if (!this.IsSameConnector(closeConnector, connector))
						{
							throw new Exception("入口接头类型不匹配");
						}
						Connector closeConnector3 = hypiping4.GetCloseConnector(point, false);
						Connector distanceConnector3 = hypiping4.GetDistanceConnector(point);
						Connector nearConnector2 = connectHelper.GetNearConnector(familyInstance2, distanceConnector3);
						if (!this.IsSameConnector(closeConnector3, nearConnector2))
						{
							throw new Exception("出口接头类型不匹配");
						}
						distanceConnector = hypiping3.GetDistanceConnector(point);
						nearConnector = connectHelper.GetNearConnector(familyInstance2, distanceConnector);
						if (!this.IsSameConnector(closeConnector2, nearConnector))
						{
							throw new Exception("支管接头类型不匹配");
						}
						hypiping.Extend(connector);
						hypiping3.Extend(nearConnector);
						hypiping4.Extend(nearConnector2);
						document.Regenerate();
						FamilyInstance familyInstance3 = familyInstance2;
						subTransaction.Commit();
						result = familyInstance3;
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewTeeFittingT(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 3 || xyzs.Count != 3)
				{
					connMSG = "三通连接必须是三条风管！";
					result = familyInstance;
				}
				else
				{
					document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "同一根风管不能构成三通连接！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第一根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (hypiping2.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第二根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (listName == null)
					{
						IList<Duct> list = new List<Duct>();
						list.Add(ducts[0]);
						if (ducts[0].Id != ducts[1].Id)
						{
							list.Add(ducts[1]);
						}
						list.Add(ducts[2]);
						this.NewSomeConnect(list);
						result = null;
					}
					else if (listName.Count <= 0)
					{
						connMSG = "请选择连接方式！";
						result = familyInstance;
					}
					else
					{
						YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
						YJKLine hyline2 = new YJKLine(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
						if (!hyline.IsVertical(hyline2.Line) && !listName[0].Contains("圆形斜角三通"))
						{
							connMSG = "第三根风管与第一根风管垂直！";
							result = familyInstance;
						}
						else
						{
							XYZ origin = hypiping.StartConnector.Origin;
							XYZ origin2 = hypiping.EndConnector.Origin;
							YJKLine hyline3 = new YJKLine(origin, origin2);
							XYZ origin3 = hypiping3.StartConnector.Origin;
							XYZ origin4 = hypiping3.EndConnector.Origin;
							YJKLineEx.YJKGetBound(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
							if (!hypiping.IsParallel(hypiping2.Piping))
							{
								connMSG = "风管2不正确，\n风管2应与风管1平行。";
								result = familyInstance;
							}
							else
							{
								XYZ xyz = YJKRevitTools.GetDLineP(origin3, origin4, origin, origin2);
								if (xyz == null)
								{
									connMSG = "风管3与风管1没有交点。";
									result = familyInstance;
								}
								else
								{
									xyz = new XYZ(xyz.X, xyz.Y, origin.Z);
									YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
									bool flag = hyline3.IsPointOnLine(xyz);
									YJKPiping<Duct> hypiping4;
									if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
									{
										if (!flag)
										{
											connMSG = "风管3的位置不正确，\n风管1与3在风管1上有交点。";
											return familyInstance;
										}
										IList<Duct> list2 = new List<Duct>();
										list2 = hypiping.Separate(xyz);
										hyrotateFamilyInstance.MovePiping(list2[0]);
										hyrotateFamilyInstance.MovePiping(list2[1]);
										document.Regenerate();
										IList<Duct> orderDucts = this.GetOrderDucts(list2, xyzs[0]);
										hypiping = new YJKPiping<Duct>(orderDucts[0]);
										hypiping4 = new YJKPiping<Duct>(orderDucts[1]);
									}
									else
									{
										hypiping4 = new YJKPiping<Duct>(hypiping2.Piping, hypiping2.GlobalPoint);
									}
									Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
									Connector closeConnector2 = hypiping4.GetCloseConnector(xyz, false);
									Connector closeConnector3 = hypiping3.GetCloseConnector(xyz, false);
									if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector2.CoordinateSystem.BasisZ, 0.0005) && !flag)
									{
										connMSG = "风管3的位置不正确，\n无法构成连接关系。";
										result = familyInstance;
									}
									else
									{
										int num = hypiping.CheckDuctsRelation(hypiping.Piping, hypiping3.Piping);
										if (num == -1)
										{
											connMSG = "没有找到连接风管！";
											result = familyInstance;
										}
										else
										{
											string text;
											if (num == 0)
											{
												text = listName[0];
											}
											else if (num == 1)
											{
												text = listName[1];
											}
											else if (num == 2)
											{
												text = listName[2];
											}
											else
											{
												text = listName[1];
											}
											string text2 = "";
											ConnectDuctManage connectDuctManage = new ConnectDuctManage();
											FamilySymbol familySymbol = connectDuctManage.GetFamilySymbol(text, document, ref text2);
											if (familySymbol == null)
											{
												if (!string.IsNullOrEmpty(text2))
												{
													connMSG = text2;
													result = familyInstance;
												}
												else
												{
													connMSG = "加载族失败！";
													result = familyInstance;
												}
											}
											else
											{
												ConnectHelper connectHelper = new ConnectHelper();
												subTransaction = new SubTransaction(document);
												subTransaction.Start();
												UnifiedModified.ActivateFamilySymbol(familySymbol);
												FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(xyz, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
												document.Regenerate();
												int num2 = this.SettingTeeSize(familyInstance2, hypiping.Piping, 1, false);
												if (num2 != 0 && num2 != 1)
												{
													connMSG = "族尺寸参数不匹配，\n设置三通入口尺寸失败！";
													result = familyInstance;
												}
												else
												{
													num2 = this.SettingTeeSize(familyInstance2, hypiping3.Piping, 3, false);
													if (num2 != 0 && num2 != 1)
													{
														connMSG = "族尺寸参数不匹配，\n设置三通入口尺寸失败！";
														result = familyInstance;
													}
													else
													{
														double num3 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-1.0 * closeConnector3.CoordinateSystem.BasisZ) * 180.0 / Math.PI;
														if (text.Contains("圆形斜角三通") && (num3 > 90.0 || num3 < 25.0))
														{
															connMSG = "支管与入口管夹角应该在90°-155°之间！";
															result = familyInstance;
														}
														else
														{
															this.SettingTeeAngle(familyInstance2, num3 * Math.PI / 180.0, 2);
															Connector connector = connectHelper.GetTeeMainConnector(familyInstance2);
															Connector connector2 = connectHelper.GetTeeOutConnector(familyInstance2, connector.CoordinateSystem.BasisZ);
															Connector connector3 = connectHelper.GetTeeSideConnector(familyInstance2, connector.CoordinateSystem.BasisZ);
															hyrotateFamilyInstance.MoveFamilyInstance(familyInstance2, connector.CoordinateSystem.BasisZ * 0.05);
															hyrotateFamilyInstance.MoveFamilyInstance(familyInstance2, closeConnector.Origin.Subtract(connector.Origin));
															Line line = YJKLineEx.YJKGetBound(xyz, xyz + new XYZ(0.0, 0.0, 1.0));
															line.MakeUnbound();
															double num4 = 0.0;
															if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
															{
																num4 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-connector.CoordinateSystem.BasisZ);
															}
															if (num4 > 0.0001)
															{
																ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, num4);
																document.Regenerate();
																if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisZ))
																{
																	ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, Math.PI*2 - 2.0 * num4);
																	document.Regenerate();
																}
															}
															if (!string.IsNullOrEmpty(connMSG))
															{
																result = familyInstance;
															}
															else
															{
																int num5 = hypiping.CheckConnLocation(connector3, closeConnector3);
																if (num5 != 1 && num5 != 0)
																{
																	IList<Connector> list3 = this.MirrorTeeFitting(connector, closeConnector2, closeConnector3, ref familyInstance2);
																	if (list3.Count <= 0)
																	{
																		connMSG = "镜像后三通没有找到！";
																		return familyInstance;
																	}
																	connector2 = list3[1];
																	connector = list3[0];
																	connector3 = list3[2];
																}
																num5 = hypiping.CheckConnLocation(connector3, closeConnector3);
																if (num5 == -1)
																{
																	connMSG = "风管3位置不正确！";
																	result = familyInstance;
																}
																else if (num5 == -2)
																{
																	connMSG = "找不到连接器！";
																	result = familyInstance;
																}
																else
																{
																	Duct elem = null;
																	closeConnector.ConnectTo(connector);
																	document.Regenerate();
																	hyrotateFamilyInstance.MovePiping(hypiping.Piping);
																	connectDuctManage.SetDuctReduceFitting(new List<Duct>
																	{
																		hypiping.Piping,
																		hypiping3.Piping,
																		hypiping4.Piping
																	});
																	if (num5 == 1)
																	{
																		closeConnector3.ConnectTo(connector3);
																	}
																	else
																	{
																		Connector connector4 = this.CreatLittlePipe(hypiping3.Piping, connector3, ref elem);
																		FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector3);
																		document.DeleteElement(elem);
																		document.Regenerate();
																		Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector3);
																		Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																		if (notConnectedConn != null && notConnectedConn2 != null)
																		{
																			notConnectedConn.ConnectTo(notConnectedConn2);
																		}
																	}
																	int num6 = hypiping.CheckConnLocation(closeConnector2, connector2);
																	if (num6 == 1)
																	{
																		if (this.IsSameSizeConn(closeConnector2, connector2))
																		{
																			closeConnector2.ConnectTo(connector2);
																		}
																		else
																		{
																			Connector connector4 = this.CreatLittlePipe(hypiping.Piping, connector2, ref elem);
																			FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector2);
																			document.DeleteElement(elem);
																			document.Regenerate();
																			Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector2);
																			Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																			if (notConnectedConn != null && notConnectedConn2 != null)
																			{
																				notConnectedConn.ConnectTo(notConnectedConn2);
																			}
																		}
																	}
																	else
																	{
																		if (num6 != 0)
																		{
																			connMSG = "连接失败！";
																			return familyInstance;
																		}
																		Connector connector4 = this.CreatLittlePipe(hypiping.Piping, connector2, ref elem);
																		FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector2);
																		document.DeleteElement(elem);
																		document.Regenerate();
																		Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector2);
																		Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																		if (notConnectedConn != null && notConnectedConn2 != null)
																		{
																			notConnectedConn.ConnectTo(notConnectedConn2);
																		}
																	}
																	double length = YJKRevitTools.getDuctWidth(hypiping3.Piping) / 2.0;
																	double length2 = YJKRevitTools.getDuctWidth(hypiping.Piping) / 2.0;
																	this.SettingFittingLength(familyInstance2, length, 1);
																	this.SettingFittingLength(familyInstance2, length, 2);
																	this.SettingFittingLength(familyInstance2, length2, 3);
																	hyrotateFamilyInstance.MovePiping(hypiping.Piping);
																	hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
																	hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
																	document.Regenerate();
																	FamilyInstance familyInstance3 = familyInstance2;
																	subTransaction.Commit();
																	result = familyInstance3;
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewTeeFittingHT(IList<Duct> ducts, IList<XYZ> xyzs, string familySymbolName, ref string connMSG, bool IsConnRiser = false)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 3 || xyzs.Count != 3)
				{
					connMSG = "三通连接必须是三条风管！";
					result = familyInstance;
				}
				else
				{
					document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "同一根风管不能构成三通连接！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第一根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (hypiping2.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第二根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else
					{
						YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
						YJKLine hyline2 = new YJKLine(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
						if (!hyline.IsVertical(hyline2.Line) && !familySymbolName.Contains("圆形斜角三通"))
						{
							connMSG = "第三根风管与第一根风管垂直！";
							result = familyInstance;
						}
						else
						{
							XYZ origin = hypiping.StartConnector.Origin;
							XYZ origin2 = hypiping.EndConnector.Origin;
							new YJKLine(origin, origin2);
							XYZ origin3 = hypiping3.StartConnector.Origin;
							XYZ origin4 = hypiping3.EndConnector.Origin;
							if (!hypiping.IsParallel(hypiping2.Piping))
							{
								connMSG = "风管2不正确，\n风管2应与风管1平行。";
								result = familyInstance;
							}
							else
							{
								XYZ dlineP = YJKRevitTools.GetDLineP(origin3, origin4, origin, origin2);
								if (dlineP == null)
								{
									connMSG = "风管3与风管1没有交点。";
									result = familyInstance;
								}
								else
								{
									Connector closeConnector = hypiping.GetCloseConnector(dlineP, false);
									Connector closeConnector2 = hypiping2.GetCloseConnector(dlineP, false);
									Connector closeConnector3 = hypiping3.GetCloseConnector(dlineP, false);
									string text = "";
									FamilySymbol familySymbol = new ConnectDuctManage().GetFamilySymbol(familySymbolName, document, ref text);
									if (familySymbol == null)
									{
										if (!string.IsNullOrEmpty(text))
										{
											connMSG = text;
											result = familyInstance;
										}
										else
										{
											connMSG = "加载族失败！";
											result = familyInstance;
										}
									}
									else
									{
										ConnectHelper connectHelper = new ConnectHelper();
										subTransaction = new SubTransaction(document);
										subTransaction.Start();
										UnifiedModified.ActivateFamilySymbol(familySymbol);
										FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(dlineP, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
										document.Regenerate();
										int num = this.SettingTeeSize(familyInstance2, hypiping.Piping, 1, IsConnRiser);
										if (num != 0 && num != 1)
										{
											connMSG = "族尺寸参数不匹配，\n设置三通入口尺寸失败！";
											result = familyInstance;
										}
										else
										{
											num = this.SettingTeeSize(familyInstance2, hypiping2.Piping, 2, IsConnRiser);
											if (num != 0 && num != 1)
											{
												connMSG = "族尺寸参数不匹配，\n设置三通出口尺寸失败！";
												result = familyInstance;
											}
											else
											{
												num = this.SettingTeeSize(familyInstance2, hypiping3.Piping, 3, false);
												if (num != 0 && num != 1)
												{
													connMSG = "族尺寸参数不匹配，\n设置三通支管尺寸失败！";
													result = familyInstance;
												}
												else
												{
													double num2 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-1.0 * closeConnector3.CoordinateSystem.BasisZ) * 180.0 / Math.PI;
													if (familySymbolName.Contains("圆形斜角三通") && (num2 > 90.0 || num2 < 25.0))
													{
														connMSG = "支管与入口管夹角应该在90°-155°之间！";
														result = familyInstance;
													}
													else
													{
														this.SettingTeeAngle(familyInstance2, num2 * Math.PI / 180.0, 2);
														YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
														Connector teeMainConnector = connectHelper.GetTeeMainConnector(familyInstance2);
														Connector teeOutConnector = connectHelper.GetTeeOutConnector(familyInstance2, teeMainConnector.CoordinateSystem.BasisZ);
														Connector teeSideConnector = connectHelper.GetTeeSideConnector(familyInstance2, teeMainConnector.CoordinateSystem.BasisZ);
														hyrotateFamilyInstance.MoveFamilyInstance(familyInstance2, teeMainConnector.CoordinateSystem.BasisZ * 0.05);
														Line line = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(1.0, 0.0, 0.0));
														line.MakeUnbound();
														double num3 = Math.PI;
														ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, num3 / 2.0);
														document.Regenerate();
														line = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(0.0, 0.0, 1.0));
														line.MakeUnbound();
														num3 = 0.0;
														if (teeMainConnector != null && closeConnector != null && !teeMainConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
														{
															num3 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-teeMainConnector.CoordinateSystem.BasisZ);
														}
														if (num3 > 0.0001)
														{
															ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, num3);
															document.Regenerate();
															if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-teeMainConnector.CoordinateSystem.BasisZ))
															{
																ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, Math.PI*2 - 2.0 * num3);
																document.Regenerate();
															}
														}
														if (!string.IsNullOrEmpty(connMSG))
														{
															result = familyInstance;
														}
														else
														{
															closeConnector.ConnectTo(teeMainConnector);
															closeConnector3.ConnectTo(teeSideConnector);
															closeConnector2.ConnectTo(teeOutConnector);
															hyrotateFamilyInstance.MovePiping(hypiping.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping2.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
															document.Regenerate();
															FamilyInstance familyInstance3 = familyInstance2;
															subTransaction.Commit();
															result = familyInstance3;
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewTeeFittingHT(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 3 || xyzs.Count != 3)
				{
					connMSG = "三通连接必须是三条风管！";
					result = familyInstance;
				}
				else
				{
					document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "同一根风管不能构成三通连接！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第一根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (hypiping2.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第二根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (listName == null)
					{
						IList<Duct> list = new List<Duct>();
						list.Add(ducts[0]);
						if (ducts[0].Id != ducts[1].Id)
						{
							list.Add(ducts[1]);
						}
						list.Add(ducts[2]);
						this.NewSomeConnect(list);
						result = null;
					}
					else if (listName.Count <= 0)
					{
						connMSG = "请选择连接方式！";
						result = familyInstance;
					}
					else
					{
						YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
						YJKLine hyline2 = new YJKLine(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
						if (!hyline.IsVertical(hyline2.Line) && !listName[0].Contains("圆形斜角三通"))
						{
							connMSG = "第三根风管与第一根风管垂直！";
							result = familyInstance;
						}
						else
						{
							XYZ origin = hypiping.StartConnector.Origin;
							XYZ origin2 = hypiping.EndConnector.Origin;
							YJKLine hyline3 = new YJKLine(origin, origin2);
							XYZ origin3 = hypiping3.StartConnector.Origin;
							XYZ origin4 = hypiping3.EndConnector.Origin;
							YJKLineEx.YJKGetBound(hypiping3.StartConnector.Origin, hypiping3.EndConnector.Origin);
							if (!hypiping.IsParallel(hypiping2.Piping))
							{
								connMSG = "风管2不正确，\n风管2应与风管1平行。";
								result = familyInstance;
							}
							else
							{
								XYZ dlineP = YJKRevitTools.GetDLineP(origin3, origin4, origin, origin2);
								if (dlineP == null)
								{
									connMSG = "风管3与风管1没有交点。";
									result = familyInstance;
								}
								else
								{
									YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
									bool flag = hyline3.IsPointOnLine(dlineP);
									YJKPiping<Duct> hypiping4;
									if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
									{
										if (!flag)
										{
											connMSG = "风管3的位置不正确，\n风管1与3在风管1上有交点。";
											return familyInstance;
										}
										IList<Duct> list2 = new List<Duct>();
										list2 = hypiping.Separate(dlineP);
										hyrotateFamilyInstance.MovePiping(list2[0]);
										hyrotateFamilyInstance.MovePiping(list2[1]);
										document.Regenerate();
										IList<Duct> orderDucts = this.GetOrderDucts(list2, xyzs[0]);
										hypiping = new YJKPiping<Duct>(orderDucts[0]);
										hypiping4 = new YJKPiping<Duct>(orderDucts[1]);
									}
									else
									{
										hypiping4 = new YJKPiping<Duct>(hypiping2.Piping, hypiping2.GlobalPoint);
									}
									Connector closeConnector = hypiping.GetCloseConnector(dlineP, false);
									Connector closeConnector2 = hypiping4.GetCloseConnector(dlineP, false);
									Connector closeConnector3 = hypiping3.GetCloseConnector(dlineP, false);
									if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector2.CoordinateSystem.BasisZ, 0.0005) && !flag)
									{
										connMSG = "风管3的位置不正确，\n无法构成连接关系。";
										result = familyInstance;
									}
									else
									{
										int num = hypiping.CheckDuctsRelation(hypiping.Piping, hypiping3.Piping);
										if (num == -1)
										{
											connMSG = "没有找到连接风管！";
											result = familyInstance;
										}
										else
										{
											string text;
											if (num == 0)
											{
												text = listName[0];
											}
											else if (num == 1)
											{
												text = listName[1];
											}
											else if (num == 2)
											{
												text = listName[2];
											}
											else
											{
												text = listName[1];
											}
											string text2 = "";
											ConnectDuctManage connectDuctManage = new ConnectDuctManage();
											FamilySymbol familySymbol = connectDuctManage.GetFamilySymbol(text, document, ref text2);
											if (familySymbol == null)
											{
												if (!string.IsNullOrEmpty(text2))
												{
													connMSG = text2;
													result = familyInstance;
												}
												else
												{
													connMSG = "加载族失败！";
													result = familyInstance;
												}
											}
											else
											{
												ConnectHelper connectHelper = new ConnectHelper();
												subTransaction = new SubTransaction(document);
												subTransaction.Start();
												UnifiedModified.ActivateFamilySymbol(familySymbol);
												FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(dlineP, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
												document.Regenerate();
												int num2 = this.SettingTeeSize(familyInstance2, hypiping.Piping, 1, false);
												if (num2 != 0 && num2 != 1)
												{
													connMSG = "族尺寸参数不匹配，\n设置三通入口尺寸失败！";
													result = familyInstance;
												}
												else
												{
													num2 = this.SettingTeeSize(familyInstance2, hypiping3.Piping, 3, false);
													if (num2 != 0 && num2 != 1)
													{
														connMSG = "族尺寸参数不匹配，\n设置三通入口尺寸失败！";
														result = familyInstance;
													}
													else
													{
														double num3 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-1.0 * closeConnector3.CoordinateSystem.BasisZ) * 180.0 / Math.PI;
														if (text.Contains("圆形斜角三通") && (num3 > 90.0 || num3 < 25.0))
														{
															connMSG = "支管与入口管夹角应该在90°-155°之间！";
															result = familyInstance;
														}
														else
														{
															this.SettingTeeAngle(familyInstance2, num3 * Math.PI / 180.0, 2);
															Connector connector = connectHelper.GetTeeMainConnector(familyInstance2);
															Connector connector2 = connectHelper.GetTeeOutConnector(familyInstance2, connector.CoordinateSystem.BasisZ);
															Connector connector3 = connectHelper.GetTeeSideConnector(familyInstance2, connector.CoordinateSystem.BasisZ);
															hyrotateFamilyInstance.MoveFamilyInstance(familyInstance2, connector.CoordinateSystem.BasisZ * 0.05);
															Line line = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(1.0, 0.0, 0.0));
															line.MakeUnbound();
															double num4 = Math.PI;
															ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, num4 / 2.0);
															document.Regenerate();
															line = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(0.0, 0.0, 1.0));
															line.MakeUnbound();
															num4 = 0.0;
															if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
															{
																num4 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-connector.CoordinateSystem.BasisZ);
															}
															if (num4 > 0.0001)
															{
																ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, num4);
																document.Regenerate();
																if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisZ))
																{
																	ElementTransformUtils.RotateElement(document, familyInstance2.Id, line, Math.PI*2 - 2.0 * num4);
																	document.Regenerate();
																}
															}
															if (!string.IsNullOrEmpty(connMSG))
															{
																result = familyInstance;
															}
															else
															{
																int num5 = hypiping.CheckConnLocation(connector3, closeConnector3);
																if (num5 != 1 && num5 != 0)
																{
																	IList<Connector> list3 = this.MirrorTeeFitting(connector, closeConnector2, closeConnector3, ref familyInstance2);
																	if (list3.Count <= 0)
																	{
																		connMSG = "镜像后三通没有找到！";
																		return familyInstance;
																	}
																	connector2 = list3[1];
																	connector = list3[0];
																	connector3 = list3[2];
																}
																num5 = hypiping.CheckConnLocation(connector3, closeConnector3);
																if (num5 == -1)
																{
																	connMSG = "风管3位置不正确！";
																	result = familyInstance;
																}
																else if (num5 == -2)
																{
																	connMSG = "找不到连接器！";
																	result = familyInstance;
																}
																else
																{
																	Duct elem = null;
																	closeConnector.ConnectTo(connector);
																	connectDuctManage.SetDuctReduceFitting(new List<Duct>
																	{
																		hypiping.Piping,
																		hypiping3.Piping,
																		hypiping4.Piping
																	});
																	if (num5 == 1)
																	{
																		closeConnector3.ConnectTo(connector3);
																	}
																	else
																	{
																		Connector connector4 = this.CreatLittlePipe(hypiping3.Piping, connector3, ref elem);
																		FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector3);
																		document.DeleteElement(elem);
																		document.Regenerate();
																		Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector3);
																		Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																		if (notConnectedConn != null && notConnectedConn2 != null)
																		{
																			notConnectedConn.ConnectTo(notConnectedConn2);
																		}
																	}
																	int num6 = hypiping.CheckConnLocation(closeConnector2, connector2);
																	if (num6 == 1)
																	{
																		if (this.IsSameSizeConn(closeConnector2, connector2))
																		{
																			closeConnector2.ConnectTo(connector2);
																		}
																		else
																		{
																			Connector connector4 = this.CreatLittlePipe(hypiping.Piping, connector2, ref elem);
																			FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector2);
																			document.DeleteElement(elem);
																			document.Regenerate();
																			Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector2);
																			Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																			if (notConnectedConn != null && notConnectedConn2 != null)
																			{
																				notConnectedConn.ConnectTo(notConnectedConn2);
																			}
																		}
																	}
																	else
																	{
																		if (num6 != 0)
																		{
																			connMSG = "连接失败！";
																			return familyInstance;
																		}
																		Connector connector4 = this.CreatLittlePipe(hypiping.Piping, connector2, ref elem);
																		FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector2);
																		document.DeleteElement(elem);
																		document.Regenerate();
																		Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector2);
																		Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																		if (notConnectedConn != null && notConnectedConn2 != null)
																		{
																			notConnectedConn.ConnectTo(notConnectedConn2);
																		}
																	}
																	double length = YJKRevitTools.getDuctWidth(hypiping3.Piping) / 2.0;
																	double length2 = YJKRevitTools.getDuctWidth(hypiping.Piping) / 2.0;
																	this.SettingFittingLength(familyInstance2, length, 1);
																	this.SettingFittingLength(familyInstance2, length, 2);
																	this.SettingFittingLength(familyInstance2, length2, 3);
																	hyrotateFamilyInstance.MovePiping(hypiping.Piping);
																	hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
																	hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
																	document.Regenerate();
																	FamilyInstance familyInstance3 = familyInstance2;
																	subTransaction.Commit();
																	result = familyInstance3;
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool IsConnecteddByRoundAngleTee(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			return new ConnectDuctsByFitting().IsConnecteddByRoundAngleTee(ducts, xyzs, listName, ref connMSG);
		}

		public bool IsConnecteddByYShapeTee(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			return new ConnectDuctsByFitting().IsConnecteddByYShapeTee(ducts, xyzs, listName, ref connMSG);
		}

		public bool IsConnecteddByCross(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			return new ConnectDuctsByFitting().IsConnecteddByCross(ducts, xyzs, listName, ref connMSG);
		}

		public FamilyInstance NewTeeFittingF(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 3 || xyzs.Count != 3)
				{
					connMSG = "三通连接必须是三条风管！";
					result = familyInstance;
				}
				else
				{
					document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					if (listName.Count <= 0)
					{
						connMSG = "请选择连接方式！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "同一根风管不能构成三通连接！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第一根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id))
					{
						connMSG = "第一根风管与第二根风管不能为同一风管！";
						result = familyInstance;
					}
					else
					{
						XYZ origin = hypiping.StartConnector.Origin;
						XYZ origin2 = hypiping.EndConnector.Origin;
						YJKLine hyline = new YJKLine(origin, origin2);
						XYZ origin3 = hypiping2.StartConnector.Origin;
						XYZ origin4 = hypiping2.EndConnector.Origin;
						Line line = YJKLineEx.YJKGetBound(origin3, origin4);
						XYZ origin5 = hypiping3.StartConnector.Origin;
						XYZ origin6 = hypiping3.EndConnector.Origin;
						YJKLine hyline2 = new YJKLine(origin5, origin6);
						if (!hyline.IsVertical(hyline2.Line))
						{
							connMSG = "风管3不正确，\n风管3应该垂直于风管1。";
							result = familyInstance;
						}
						else if (!hyline.IsVertical(line))
						{
							connMSG = "风管2不正确，\n风管2应垂直于风管1。";
							result = familyInstance;
						}
						else
						{
							XYZ dlineP = YJKRevitTools.GetDLineP(origin5, origin6, origin, origin2);
							XYZ dlineP2 = YJKRevitTools.GetDLineP(origin, origin2, origin5, origin6);
							if (dlineP == null || dlineP2 == null)
							{
								connMSG = "风管3与风管1没有交点。";
								result = familyInstance;
							}
							else
							{
								bool flag = hyline2.IsPointOnLine(dlineP2);
								YJKPiping<Duct> hypiping4;
								if (hypiping3.Piping.UniqueId == hypiping2.Piping.UniqueId)
								{
									if (!flag)
									{
										connMSG = "风管1的位置不正确，\n风管1与3在风管3上有交点。";
										return familyInstance;
									}
									new List<Duct>();
									List<Duct> list = hypiping3.Separate(dlineP2);
									hypiping4 = new YJKPiping<Duct>(((IList<Duct>)list)[1], hypiping2.GlobalPoint);
									hypiping3 = new YJKPiping<Duct>(((IList<Duct>)list)[0], hypiping2.GlobalPoint);
									document.Regenerate();
								}
								else
								{
									hypiping4 = new YJKPiping<Duct>(hypiping2.Piping, hypiping2.GlobalPoint);
								}
								Connector closeConnector = hypiping.GetCloseConnector(dlineP, false);
								Connector closeConnector2 = hypiping4.GetCloseConnector(dlineP, false);
								Connector closeConnector3 = hypiping3.GetCloseConnector(dlineP, false);
								if (!closeConnector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector2.CoordinateSystem.BasisZ, 0.0005))
								{
									connMSG = "风管3的位置不正确，\n无法构成连接关系。";
									result = familyInstance;
								}
								else
								{
									int num = hypiping.CheckDuctsRelation(hypiping.Piping, hypiping3.Piping);
									if (num == -1)
									{
										connMSG = "没有找到连接风管！";
										result = familyInstance;
									}
									else
									{
										string fsName;
										if (num == 0)
										{
											fsName = listName[0];
										}
										else if (num == 1)
										{
											fsName = listName[1];
										}
										else if (num == 2)
										{
											fsName = listName[2];
										}
										else
										{
											fsName = listName[1];
										}
										string text = "";
										ConnectDuctManage connectDuctManage = new ConnectDuctManage();
										FamilySymbol familySymbol = connectDuctManage.GetFamilySymbol(fsName, document, ref text);
										if (familySymbol == null)
										{
											if (!string.IsNullOrEmpty(text))
											{
												connMSG = text;
												result = familyInstance;
											}
											else
											{
												connMSG = "加载族失败！";
												result = familyInstance;
											}
										}
										else
										{
											ConnectHelper connectHelper = new ConnectHelper();
											subTransaction = new SubTransaction(document);
											subTransaction.Start();
											YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
											UnifiedModified.ActivateFamilySymbol(familySymbol);
											FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(dlineP, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
											document.Regenerate();
											int num2 = this.SettingTeeSize(familyInstance2, hypiping.Piping, 1, false);
											if (num2 != 0 && num2 != 1)
											{
												connMSG = "族尺寸参数不匹配，\n设置三通入口尺寸失败！";
												result = familyInstance;
											}
											else
											{
												num2 = this.SettingTeeSize(familyInstance2, hypiping4.Piping, 2, false);
												if (num2 != 0 && num2 != 1)
												{
													connMSG = "族尺寸参数不匹配，\n设置三通出口尺寸失败！";
													result = familyInstance;
												}
												else
												{
													num2 = this.SettingTeeSize(familyInstance2, hypiping3.Piping, 3, false);
													Connector connector = connectHelper.GetTeeMainConnector(familyInstance2);
													Connector connector2 = connectHelper.GetTeeOutConnector(familyInstance2, new XYZ(0.0, -1.0, 0.0));
													Connector connector3 = connectHelper.GetTeeOutConnector(familyInstance2, new XYZ(0.0, 1.0, 0.0));
													hyrotateFamilyInstance.MoveFamilyInstance(familyInstance2, connector.CoordinateSystem.BasisZ * 0.05);
													Line line2 = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(0.0, 0.0, 1.0));
													line2.MakeUnbound();
													double num3 = 0.0;
													if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
													{
														num3 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-connector.CoordinateSystem.BasisZ);
													}
													if (num3 > 0.0001)
													{
														ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, num3);
														document.Regenerate();
														if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisZ))
														{
															ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, Math.PI*2 - 2.0 * num3);
															document.Regenerate();
														}
													}
													if (!string.IsNullOrEmpty(connMSG))
													{
														result = familyInstance;
													}
													else
													{
														int num4 = hypiping.CheckConnLocation(connector3, closeConnector3);
														if (num4 != 1 && num4 != 0)
														{
															IList<Connector> list2 = this.MirrorTeeFitting(connector, closeConnector2, closeConnector3, ref familyInstance2);
															if (list2.Count <= 0)
															{
																connMSG = "镜像后三通没有找到！";
																return familyInstance;
															}
															connector2 = list2[1];
															connector = list2[0];
															connector3 = list2[2];
														}
														num4 = hypiping.CheckConnLocation(connector3, closeConnector3);
														if (num4 == -1)
														{
															connMSG = "风管3位置不正确！";
															result = familyInstance;
														}
														else if (num4 == -2)
														{
															connMSG = "找不到连接器！";
															result = familyInstance;
														}
														else
														{
															Duct elem = null;
															closeConnector.ConnectTo(connector);
															document.Regenerate();
															hyrotateFamilyInstance.MovePiping(hypiping.Piping);
															connectDuctManage.SetDuctReduceFitting(new List<Duct>
															{
																hypiping.Piping,
																hypiping3.Piping,
																hypiping4.Piping
															});
															if (num4 == 1)
															{
																if (this.IsSameSizeConn(connector3, closeConnector3))
																{
																	closeConnector3.ConnectTo(connector3);
																}
																else
																{
																	Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector3, ref elem);
																	FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector3);
																	document.DeleteElement(elem);
																	document.Regenerate();
																	Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector3);
																	Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																	if (notConnectedConn != null && notConnectedConn2 != null)
																	{
																		notConnectedConn.ConnectTo(notConnectedConn2);
																	}
																}
															}
															else
															{
																Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector3, ref elem);
																FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector3);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector3);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															int num5 = hypiping.CheckConnLocation(closeConnector2, connector2);
															if (num5 == 1)
															{
																closeConnector2.ConnectTo(connector2);
															}
															else
															{
																if (num5 != 0)
																{
																	connMSG = "连接失败！";
																	return familyInstance;
																}
																Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector2, ref elem);
																FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector2);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector2);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															this.SettingTeeSize(familyInstance2, this.GetMaxDuctSize(hypiping3.Piping, hypiping4.Piping));
															double length = hypiping3.GetDiameter() / 2.0;
															double length2 = hypiping.GetDiameter() / 2.0;
															this.SettingFittingLength(familyInstance2, length, 1);
															this.SettingFittingLength(familyInstance2, length, 2);
															this.SettingFittingLength(familyInstance2, length2, 3);
															hyrotateFamilyInstance.MovePiping(hypiping.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
															document.Regenerate();
															FamilyInstance familyInstance3 = familyInstance2;
															subTransaction.Commit();
															result = familyInstance3;
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewTeeFittingHF(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 3 || xyzs.Count != 3)
				{
					connMSG = "三通连接必须是三条风管！";
					result = familyInstance;
				}
				else
				{
					document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					if (listName.Count <= 0)
					{
						connMSG = "请选择连接方式！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "同一根风管不能构成三通连接！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第一根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id))
					{
						connMSG = "第一根风管与第二根风管不能为同一风管！";
						result = familyInstance;
					}
					else
					{
						XYZ origin = hypiping.StartConnector.Origin;
						XYZ origin2 = hypiping.EndConnector.Origin;
						YJKLine hyline = new YJKLine(origin, origin2);
						XYZ origin3 = hypiping2.StartConnector.Origin;
						XYZ origin4 = hypiping2.EndConnector.Origin;
						Line line = YJKLineEx.YJKGetBound(origin3, origin4);
						XYZ origin5 = hypiping3.StartConnector.Origin;
						XYZ origin6 = hypiping3.EndConnector.Origin;
						YJKLine hyline2 = new YJKLine(origin5, origin6);
						if (!hyline.IsVertical(hyline2.Line))
						{
							connMSG = "风管3不正确，\n风管3应该垂直于风管1。";
							result = familyInstance;
						}
						else if (!hyline.IsVertical(line))
						{
							connMSG = "风管2不正确，\n风管2应垂直于风管1。";
							result = familyInstance;
						}
						else
						{
							XYZ dlineP = YJKRevitTools.GetDLineP(origin5, origin6, origin, origin2);
							XYZ dlineP2 = YJKRevitTools.GetDLineP(origin, origin2, origin5, origin6);
							if (dlineP == null || dlineP2 == null)
							{
								connMSG = "风管3与风管1没有交点。";
								result = familyInstance;
							}
							else
							{
								bool flag = hyline2.IsPointOnLine(dlineP2);
								YJKPiping<Duct> hypiping4;
								if (hypiping3.Piping.UniqueId == hypiping2.Piping.UniqueId)
								{
									if (!flag)
									{
										connMSG = "风管1的位置不正确，\n风管1与3在风管3上有交点。";
										return familyInstance;
									}
									new List<Duct>();
									List<Duct> list = hypiping3.Separate(dlineP2);
									hypiping4 = new YJKPiping<Duct>(((IList<Duct>)list)[1], hypiping2.GlobalPoint);
									hypiping3 = new YJKPiping<Duct>(((IList<Duct>)list)[0], hypiping2.GlobalPoint);
									document.Regenerate();
								}
								else
								{
									hypiping4 = new YJKPiping<Duct>(hypiping2.Piping, hypiping2.GlobalPoint);
								}
								Connector closeConnector = hypiping.GetCloseConnector(dlineP, false);
								Connector closeConnector2 = hypiping4.GetCloseConnector(dlineP, false);
								Connector closeConnector3 = hypiping3.GetCloseConnector(dlineP, false);
								if (!closeConnector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector2.CoordinateSystem.BasisZ, 0.0005))
								{
									connMSG = "风管3的位置不正确，\n无法构成连接关系。";
									result = familyInstance;
								}
								else
								{
									int num = hypiping.CheckDuctsRelation(hypiping.Piping, hypiping3.Piping);
									if (num == -1)
									{
										connMSG = "没有找到连接风管！";
										result = familyInstance;
									}
									else
									{
										string fsName;
										if (num == 0)
										{
											fsName = listName[0];
										}
										else if (num == 1)
										{
											fsName = listName[1];
										}
										else if (num == 2)
										{
											fsName = listName[2];
										}
										else
										{
											fsName = listName[1];
										}
										string text = "";
										ConnectDuctManage connectDuctManage = new ConnectDuctManage();
										FamilySymbol familySymbol = connectDuctManage.GetFamilySymbol(fsName, document, ref text);
										if (familySymbol == null)
										{
											if (!string.IsNullOrEmpty(text))
											{
												connMSG = text;
												result = familyInstance;
											}
											else
											{
												connMSG = "加载族失败！";
												result = familyInstance;
											}
										}
										else
										{
											ConnectHelper connectHelper = new ConnectHelper();
											subTransaction = new SubTransaction(document);
											subTransaction.Start();
											YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
											UnifiedModified.ActivateFamilySymbol(familySymbol);
											FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(dlineP, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
											document.Regenerate();
											int num2 = this.SettingTeeSize(familyInstance2, hypiping.Piping, 1, false);
											if (num2 != 0 && num2 != 1)
											{
												connMSG = "族尺寸参数不匹配，\n设置三通入口尺寸失败！";
												result = familyInstance;
											}
											else
											{
												num2 = this.SettingTeeSize(familyInstance2, hypiping4.Piping, 2, false);
												if (num2 != 0 && num2 != 1)
												{
													connMSG = "族尺寸参数不匹配，\n设置三通出口尺寸失败！";
													result = familyInstance;
												}
												else
												{
													num2 = this.SettingTeeSize(familyInstance2, hypiping3.Piping, 3, false);
													Connector connector = connectHelper.GetTeeMainConnector(familyInstance2);
													Connector connector2 = connectHelper.GetTeeOutConnector(familyInstance2, new XYZ(0.0, -1.0, 0.0));
													Connector connector3 = connectHelper.GetTeeOutConnector(familyInstance2, new XYZ(0.0, 1.0, 0.0));
													hyrotateFamilyInstance.MoveFamilyInstance(familyInstance2, connector.CoordinateSystem.BasisZ * 0.05);
													Line line2 = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(0.0, 1.0, 0.0));
													line2.MakeUnbound();
													double num3 = Math.PI*.5;
													ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, num3);
													document.Regenerate();
													if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(closeConnector.CoordinateSystem.BasisZ))
													{
														line2 = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(1.0, 0.0, 0.0));
														line2.MakeUnbound();
														num3 = Math.PI;
														ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, num3);
														document.Regenerate();
													}
													line2 = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(0.0, 0.0, 1.0));
													line2.MakeUnbound();
													num3 = 0.0;
													if (connector2 != null && closeConnector2 != null && !connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector2.CoordinateSystem.BasisZ))
													{
														num3 = closeConnector2.CoordinateSystem.BasisZ.AngleTo(-connector2.CoordinateSystem.BasisZ);
													}
													if (num3 > 0.0001)
													{
														ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, num3);
														document.Regenerate();
														if (!closeConnector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector2.CoordinateSystem.BasisZ))
														{
															ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, Math.PI*2 - 2.0 * num3);
															document.Regenerate();
														}
													}
													if (!string.IsNullOrEmpty(connMSG))
													{
														result = familyInstance;
													}
													else
													{
														int num4 = hypiping.CheckConnLocation(connector3, closeConnector3);
														if (num4 != 1 && num4 != 0)
														{
															IList<Connector> list2 = this.MirrorTeeFitting(connector, closeConnector2, closeConnector3, ref familyInstance2);
															if (list2.Count <= 0)
															{
																connMSG = "镜像后三通没有找到！";
																return familyInstance;
															}
															connector2 = list2[1];
															connector = list2[0];
															connector3 = list2[2];
														}
														num4 = hypiping.CheckConnLocation(connector3, closeConnector3);
														if (num4 == -1)
														{
															connMSG = "风管3位置不正确！";
															result = familyInstance;
														}
														else if (num4 == -2)
														{
															connMSG = "找不到连接器！";
															result = familyInstance;
														}
														else
														{
															Duct elem = null;
															closeConnector.ConnectTo(connector);
															connectDuctManage.SetDuctReduceFitting(new List<Duct>
															{
																hypiping.Piping,
																hypiping3.Piping,
																hypiping4.Piping
															});
															if (num4 == 1)
															{
																if (this.IsSameSizeConn(connector3, closeConnector3))
																{
																	closeConnector3.ConnectTo(connector3);
																}
																else
																{
																	Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector3, ref elem);
																	FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector3);
																	document.DeleteElement(elem);
																	document.Regenerate();
																	Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector3);
																	Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																	if (notConnectedConn != null && notConnectedConn2 != null)
																	{
																		notConnectedConn.ConnectTo(notConnectedConn2);
																	}
																}
															}
															else
															{
																Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector3, ref elem);
																FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector3);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector3);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															int num5 = hypiping.CheckConnLocation(closeConnector2, connector2);
															if (num5 == 1)
															{
																closeConnector2.ConnectTo(connector2);
															}
															else
															{
																if (num5 != 0)
																{
																	connMSG = "连接失败！";
																	return familyInstance;
																}
																Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector2, ref elem);
																FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector2);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector2);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															this.SettingTeeSize(familyInstance2, this.GetMaxDuctSize(hypiping3.Piping, hypiping4.Piping));
															double length = hypiping3.GetDiameter() / 2.0;
															double length2 = hypiping.GetDiameter() / 2.0;
															this.SettingFittingLength(familyInstance2, length, 1);
															this.SettingFittingLength(familyInstance2, length, 2);
															this.SettingFittingLength(familyInstance2, length2, 3);
															hyrotateFamilyInstance.MovePiping(hypiping.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
															document.Regenerate();
															FamilyInstance familyInstance3 = familyInstance2;
															subTransaction.Commit();
															result = familyInstance3;
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public void RotateFamilyInstance(FamilyInstance terminal, XYZ faceVector, XYZ pointOnDuctLine)
		{
			XYZ xyz = new XYZ(0.0, 0.0, 1.0);
			double num = 0.0001;
			XYZ xyz2 = new XYZ(0.0, 0.0, 1.0);
			XYZ basisZ = YJKRevitTools.GetElementConnector((Domain)1, terminal).CoordinateSystem.BasisZ;
			XYZ point = (terminal.Location as LocationPoint).Point;
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			if (faceVector.IsAlmostEqualTo(xyz, num) || faceVector.IsAlmostEqualTo(xyz * -1.0, num))
			{
				xyz2 = basisZ.CrossProduct(xyz).Normalize();
			}
			double twoVectorAngle = YJKRevitTools.GetTwoVectorAngle(basisZ, faceVector * -1.0, xyz2);
			hyrotateFamilyInstance.RotateElem(terminal, point, xyz2, twoVectorAngle * 180.0 / Math.PI);
			this.MoveTerminal(terminal, faceVector, pointOnDuctLine);
		}

		private void MoveTerminal(FamilyInstance terminal, XYZ faceVector, XYZ pointOnDuctLine)
		{
			Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, terminal);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			YJKLine hyline = new YJKLine(pointOnDuctLine, pointOnDuctLine.Add(2.0 * faceVector));
			hyline.MakeUnBound();
			XYZ origin = elementConnector.Origin;
			XYZ moveVector = hyline.GetProject(origin).Subtract(origin);
			hyrotateFamilyInstance.MoveElem(moveVector, terminal);
		}

		public FamilyInstance NewTeeFittingY(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 3 || xyzs.Count != 3)
				{
					connMSG = "三通连接必须是三条风管！";
					result = familyInstance;
				}
				else
				{
					document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "同一根风管不能构成三通连接！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第一根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id))
					{
						connMSG = "第一根风管与第二根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (hypiping2.Piping.Id.Equals(hypiping3.Piping.Id))
					{
						connMSG = "第二根风管与第三根风管不能为同一风管！";
						result = familyInstance;
					}
					else if (listName == null)
					{
						IList<Duct> list = new List<Duct>();
						list.Add(ducts[0]);
						if (ducts[0].Id != ducts[1].Id)
						{
							list.Add(ducts[1]);
						}
						list.Add(ducts[2]);
						this.NewSomeConnect(list);
						result = null;
					}
					else if (listName.Count <= 0)
					{
						connMSG = "请选择连接方式！";
						result = familyInstance;
					}
					else
					{
						XYZ origin = hypiping.StartConnector.Origin;
						XYZ origin2 = hypiping.EndConnector.Origin;
						YJKLine hyline = new YJKLine(origin, origin2);
						XYZ origin3 = hypiping2.StartConnector.Origin;
						XYZ origin4 = hypiping2.EndConnector.Origin;
						Line line = YJKLineEx.YJKGetBound(origin3, origin4);
						XYZ origin5 = hypiping3.StartConnector.Origin;
						XYZ origin6 = hypiping3.EndConnector.Origin;
						YJKLine hyline2 = new YJKLine(origin5, origin6);
						if (hyline.IsVertical(hyline2.Line) || hyline.IsParallel(hyline2.Line))
						{
							connMSG = "风管3不正确，\n风管3不能垂直或平行于风管1。";
							result = familyInstance;
						}
						else if (hyline.IsVertical(line) || hyline.IsParallel(line))
						{
							connMSG = "风管2不正确，\n风管2不能垂直或平行于风管1。";
							result = familyInstance;
						}
						else
						{
							XYZ dlineP = YJKRevitTools.GetDLineP(origin5, origin6, origin, origin2);
							if (dlineP == null)
							{
								connMSG = "风管3与风管1没有交点。";
								result = familyInstance;
							}
							else if (hypiping3.Piping.UniqueId == hypiping2.Piping.UniqueId)
							{
								connMSG = "风管2与风管3不能为同一根风管。";
								result = familyInstance;
							}
							else
							{
								YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(hypiping2.Piping, hypiping2.GlobalPoint);
								Connector closeConnector = hypiping.GetCloseConnector(dlineP, false);
								Connector closeConnector2 = hypiping4.GetCloseConnector(dlineP, false);
								Connector closeConnector3 = hypiping3.GetCloseConnector(dlineP, false);
								XYZ basisZ = closeConnector.CoordinateSystem.BasisZ;
								XYZ basisZ2 = closeConnector2.CoordinateSystem.BasisZ;
								XYZ basisZ3 = closeConnector3.CoordinateSystem.BasisZ;
								double num = basisZ.AngleTo(basisZ2) * 180.0 / Math.PI;
								double num2 = basisZ.AngleTo(basisZ3) * 180.0 / Math.PI;
								double num3 = basisZ2.AngleTo(basisZ3) * 180.0 / Math.PI;
								if (num - 119.0 < 0.0 || num - 151.0 > 0.0)
								{
									connMSG = "风管2的位置不正确，\n其与风管1夹角应在120°与150°之间。";
									result = familyInstance;
								}
								else if (num2 - 119.0 < 0.0 || num2 - 151.0 > 0.0)
								{
									connMSG = "风管3的位置不正确，\n其与风管1夹角应在120°与150°之间。";
									result = familyInstance;
								}
								else if (Math.Abs(num + num2 + num3 - 360.0) > 0.0001)
								{
									connMSG = "请检查风管位置，\n风管2与风管3不能在风管的同一侧。";
									result = familyInstance;
								}
								else
								{
									int num4 = hypiping.CheckDuctsRelation(hypiping.Piping, hypiping3.Piping);
									if (num4 == -1)
									{
										connMSG = "没有找到连接风管！";
										result = familyInstance;
									}
									else
									{
										string fsName;
										if (num4 == 0)
										{
											fsName = listName[0];
										}
										else if (num4 == 1)
										{
											fsName = listName[1];
										}
										else if (num4 == 2)
										{
											fsName = listName[2];
										}
										else
										{
											fsName = listName[1];
										}
										string text = "";
										ConnectDuctManage connectDuctManage = new ConnectDuctManage();
										FamilySymbol familySymbol = connectDuctManage.GetFamilySymbol(fsName, document, ref text);
										if (familySymbol == null)
										{
											if (!string.IsNullOrEmpty(text))
											{
												connMSG = text;
												result = familyInstance;
											}
											else
											{
												connMSG = "加载族失败！";
												result = familyInstance;
											}
										}
										else
										{
											ConnectHelper connectHelper = new ConnectHelper();
											subTransaction = new SubTransaction(document);
											subTransaction.Start();
											YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
											UnifiedModified.ActivateFamilySymbol(familySymbol);
											FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(dlineP, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
											document.Regenerate();
											Connector connector = connectHelper.GetTeeMainConnector(familyInstance2);
											XYZ xyz = new XYZ(Math.Sqrt(2.0) / 2.0, Math.Sqrt(2.0) / 2.0, 0.0);
											XYZ xyz2 = new XYZ(Math.Sqrt(2.0) / 2.0, -1.0 * Math.Sqrt(2.0) / 2.0, 0.0);
											Connector connector2 = connectHelper.GetTeeOutConnector(familyInstance2, -1.0 * xyz);
											Connector connector3 = connectHelper.GetTeeOutConnector(familyInstance2, -1.0 * xyz2);
											int num5 = this.SettingTeeSize(familyInstance2, hypiping.Piping, 1, false);
											if (num5 != 0 && num5 != 1)
											{
												connMSG = "入口尺寸参数没有或者只读，\n设置三通尺寸失败！";
												result = familyInstance;
											}
											else
											{
												num5 = this.SettingTeeSize(familyInstance2, hypiping4.Piping, 2, false);
												if (num5 != 0 && num5 != 1)
												{
													connMSG = "出口尺寸参数没有或者只读，\n设置三通尺寸失败！";
													result = familyInstance;
												}
												else
												{
													num5 = this.SettingTeeAngle(familyInstance2, (180.0 - num) * Math.PI / 180.0, 5);
													if (num5 != 0)
													{
														connMSG = "出口角度参数没有或者只读，\n设置三通尺寸失败！";
														result = familyInstance;
													}
													else
													{
														num5 = this.SettingTeeSize(familyInstance2, hypiping4.Piping, 3, false);
														if (num5 != 0 && num5 != 1)
														{
															connMSG = "支管尺寸参数没有或者只读，\n设置三通尺寸失败！";
															result = familyInstance;
														}
														else
														{
															num5 = this.SettingTeeAngle(familyInstance2, (180.0 - num2) * Math.PI / 180.0, 6);
															if (num5 != 0)
															{
																connMSG = "支管角度参数没有或者只读，\n设置三通尺寸失败！";
																result = familyInstance;
															}
															else
															{
																hyrotateFamilyInstance.MoveFamilyInstance(familyInstance2, connector.CoordinateSystem.BasisZ * 0.05);
																Line line2 = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(0.0, 0.0, 1.0));
																line2.MakeUnbound();
																double num6 = 0.0;
																if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
																{
																	num6 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-connector.CoordinateSystem.BasisZ);
																}
																if (num6 > 0.0001)
																{
																	ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, num6);
																	document.Regenerate();
																	if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisZ))
																	{
																		ElementTransformUtils.RotateElement(document, familyInstance2.Id, line2, Math.PI*2 - 2.0 * num6);
																		document.Regenerate();
																	}
																}
																if (!string.IsNullOrEmpty(connMSG))
																{
																	result = familyInstance;
																}
																else
																{
																	int num7 = hypiping.CheckConnLocation(connector3, closeConnector3);
																	if (num7 != 1 && num7 != 0)
																	{
																		IList<Connector> list2 = this.MirrorTeeFitting(connector, closeConnector2, closeConnector3, ref familyInstance2);
																		if (list2.Count <= 0)
																		{
																			connMSG = "镜像后三通没有找到！";
																			return familyInstance;
																		}
																		connector2 = list2[1];
																		connector = list2[0];
																		connector3 = list2[2];
																	}
																	num7 = hypiping.CheckConnLocation(connector3, closeConnector3);
																	if (num7 == -1)
																	{
																		connMSG = "风管3位置不正确！";
																		result = familyInstance;
																	}
																	else if (num7 == -2)
																	{
																		connMSG = "找不到连接器！";
																		result = familyInstance;
																	}
																	else
																	{
																		Duct elem = null;
																		closeConnector.ConnectTo(connector);
																		connectDuctManage.SetDuctReduceFitting(new List<Duct>
																		{
																			hypiping.Piping,
																			hypiping3.Piping,
																			hypiping4.Piping
																		});
																		if (num7 == 1)
																		{
																			if (this.IsSameSizeConn(connector3, closeConnector3))
																			{
																				closeConnector3.ConnectTo(connector3);
																			}
																			else
																			{
																				Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector3, ref elem);
																				FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector3);
																				document.DeleteElement(elem);
																				document.Regenerate();
																				Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector3);
																				Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																				if (notConnectedConn != null && notConnectedConn2 != null)
																				{
																					notConnectedConn.ConnectTo(notConnectedConn2);
																				}
																			}
																		}
																		else
																		{
																			Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector3, ref elem);
																			FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector3);
																			document.DeleteElement(elem);
																			document.Regenerate();
																			Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector3);
																			Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																			if (notConnectedConn != null && notConnectedConn2 != null)
																			{
																				notConnectedConn.ConnectTo(notConnectedConn2);
																			}
																		}
																		int num8 = hypiping.CheckConnLocation(closeConnector2, connector2);
																		if (num8 == 1)
																		{
																			if (this.IsSameSizeConn(closeConnector2, connector2))
																			{
																				closeConnector2.ConnectTo(connector2);
																			}
																			else
																			{
																				Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector2, ref elem);
																				FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector2);
																				document.DeleteElement(elem);
																				document.Regenerate();
																				Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector2);
																				Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																				if (notConnectedConn != null && notConnectedConn2 != null)
																				{
																					notConnectedConn.ConnectTo(notConnectedConn2);
																				}
																			}
																		}
																		else
																		{
																			if (num8 != 0)
																			{
																				connMSG = "连接失败！";
																				return familyInstance;
																			}
																			Connector connector4 = this.CreatLittlePipe(hypiping4.Piping, connector2, ref elem);
																			FamilyInstance fi = document.Create.NewTransitionFitting(connector4, closeConnector2);
																			document.DeleteElement(elem);
																			document.Regenerate();
																			Connector notConnectedConn = this.GetNotConnectedConn(familyInstance2, closeConnector2);
																			Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																			if (notConnectedConn != null && notConnectedConn2 != null)
																			{
																				notConnectedConn.ConnectTo(notConnectedConn2);
																			}
																		}
																		this.SettingTeeSize(familyInstance2, this.GetMaxDuctSize(hypiping3.Piping, hypiping4.Piping));
																		hyrotateFamilyInstance.MovePiping(hypiping.Piping);
																		hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
																		hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
																		document.Regenerate();
																		FamilyInstance familyInstance3 = familyInstance2;
																		subTransaction.Commit();
																		result = familyInstance3;
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		private IList<Duct> GetOrderDucts(IList<Duct> listDuct, XYZ pickPoint)
		{
			IList<Duct> list = new List<Duct>();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			double num = hypiping.GetProject(pickPoint).DistanceTo(pickPoint);
			double num2 = hypiping2.GetProject(pickPoint).DistanceTo(pickPoint);
			if (num > num2)
			{
				list.Add(listDuct[1]);
				list.Add(listDuct[0]);
			}
			else
			{
				list.Add(listDuct[0]);
				list.Add(listDuct[1]);
			}
			return list;
		}

		private IList<Connector> MirrorTeeFitting(Connector teeMainConn, Connector teeOutConn, Connector teeSideConn, ref FamilyInstance fiTee)
		{
			Autodesk.Revit.DB.Document document = fiTee.Document;
			IList<Connector> list = new List<Connector>();
			HYSystemElementFilter hysystemElementFilter = new HYSystemElementFilter(document);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			IList<Element> allDuctFitting = hysystemElementFilter.GetAllDuctFitting();
			hyrotateFamilyInstance.MirrorFamilyInstance(fiTee, teeMainConn.CoordinateSystem.BasisZ, teeMainConn.CoordinateSystem.BasisY, teeMainConn.Origin);
			fiTee.Document.Regenerate();
			IList<Element> allDuctFitting2 = hysystemElementFilter.GetAllDuctFitting();
			FamilyInstance familyInstance = hysystemElementFilter.GetNewDuctFitting(allDuctFitting, allDuctFitting2) as FamilyInstance;
			if (familyInstance == null)
			{
				return list;
			}
			new ConnectHelper();
			list.Add(this.GetTeeOutConnector(familyInstance, -1.0 * teeMainConn.CoordinateSystem.BasisZ));
			list.Add(this.GetTeeOutConnector(familyInstance, teeOutConn.CoordinateSystem.BasisZ));
			list.Add(this.GetTeeOutConnector(familyInstance, teeSideConn.CoordinateSystem.BasisZ));
			FamilyInstance elem = fiTee;
			document.DeleteElement(elem);
			fiTee = familyInstance;
			return list;
		}

		private IList<Connector> MirrorTeeFitting(ref FamilyInstance fiTee, Connector teeMainConn, Connector teeOutConn, Connector teeSideConn)
		{
			Autodesk.Revit.DB.Document document = fiTee.Document;
			IList<Connector> list = new List<Connector>();
			HYSystemElementFilter hysystemElementFilter = new HYSystemElementFilter(document);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			IList<Element> allDuctFitting = hysystemElementFilter.GetAllDuctFitting();
			hyrotateFamilyInstance.MirrorFamilyInstance(fiTee, teeMainConn.CoordinateSystem.BasisZ, teeMainConn.CoordinateSystem.BasisY, teeMainConn.Origin);
			fiTee.Document.Regenerate();
			IList<Element> allDuctFitting2 = hysystemElementFilter.GetAllDuctFitting();
			FamilyInstance familyInstance = hysystemElementFilter.GetNewDuctFitting(allDuctFitting, allDuctFitting2) as FamilyInstance;
			if (familyInstance == null)
			{
				return list;
			}
			ConnectHelper connectHelper = new ConnectHelper();
			list.Add(connectHelper.GetTeeOutConnector(familyInstance, -1.0 * teeMainConn.CoordinateSystem.BasisZ));
			list.Add(connectHelper.GetTeeOutConnector(familyInstance, -1.0 * teeOutConn.CoordinateSystem.BasisZ));
			list.Add(connectHelper.GetTeeSideConnector(familyInstance, teeMainConn.CoordinateSystem.BasisZ));
			FamilyInstance elem = fiTee;
			document.DeleteElement(elem);
			fiTee = familyInstance;
			return list;
		}

		private Connector GetTeeOutConnector(FamilyInstance fi, XYZ vector)
		{
			Connector result = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-vector, 0.01))
					{
						result = connector;
					}
				}
			}
			return result;
		}

		private Connector GetTeeSideConnector(FamilyInstance fi, XYZ vector)
		{
			Connector result = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-vector) && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(vector))
					{
						result = connector;
					}
				}
			}
			return result;
		}

		private IList<Connector> MirrorCrossFitting(ref FamilyInstance fiCross, Connector crossMainConn, Connector crossOutConn, Connector crossSideConn1, Connector crossSideConn2)
		{
            Autodesk.Revit.DB.Document document = fiCross.Document;
			IList<Connector> list = new List<Connector>();
			HYSystemElementFilter hysystemElementFilter = new HYSystemElementFilter(document);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			IList<Element> allDuctFitting = hysystemElementFilter.GetAllDuctFitting();
			hyrotateFamilyInstance.MirrorFamilyInstance(fiCross, crossMainConn.CoordinateSystem.BasisZ, crossMainConn.CoordinateSystem.BasisY, crossMainConn.Origin);
			fiCross.Document.Regenerate();
			IList<Element> allDuctFitting2 = hysystemElementFilter.GetAllDuctFitting();
			FamilyInstance familyInstance = hysystemElementFilter.GetNewDuctFitting(allDuctFitting, allDuctFitting2) as FamilyInstance;
			if (familyInstance == null)
			{
				return list;
			}
			ConnectHelper connectHelper = new ConnectHelper();
			list.Add(connectHelper.GetTeeOutConnector(familyInstance, -1.0 * crossMainConn.CoordinateSystem.BasisZ));
			list.Add(connectHelper.GetTeeOutConnector(familyInstance, -1.0 * crossOutConn.CoordinateSystem.BasisZ));
			list.Add(this.GetTeeOutConnector(familyInstance, crossSideConn1.CoordinateSystem.BasisZ));
			list.Add(this.GetTeeOutConnector(familyInstance, crossSideConn2.CoordinateSystem.BasisZ));
			FamilyInstance elem = fiCross;
			document.DeleteElement(elem);
			fiCross = familyInstance;
			return list;
		}

		public FamilyInstance NewCrossFitting(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance2 = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 4)
				{
					connMSG = "请选择四根风管，才可以进行四通连接！";
					result = familyInstance;
				}
				else
				{
					Autodesk.Revit.DB.Document document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[3], xyzs[3]);
					if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id) && hypiping.Piping.Id.Equals(hypiping4.Piping.Id))
					{
						connMSG = "同一根风管不能构成四通连接！";
						result = familyInstance2;
					}
					else if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id) || hypiping.Piping.Id.Equals(hypiping4.Piping.Id))
					{
						connMSG = "第一根风管与第三根或第四根风管不能为同一风管！";
						result = familyInstance2;
					}
					else if (hypiping2.Piping.Id.Equals(hypiping3.Piping.Id) || hypiping2.Piping.Id.Equals(hypiping4.Piping.Id))
					{
						connMSG = "第二根风管与第三根或第四根风管不能为同一风管！";
						result = familyInstance2;
					}
					else
					{
						XYZ origin = hypiping.StartConnector.Origin;
						XYZ origin2 = hypiping.EndConnector.Origin;
						YJKLine hyline = new YJKLine(origin, origin2);
						XYZ origin3 = hypiping2.StartConnector.Origin;
						XYZ origin4 = hypiping2.EndConnector.Origin;
						YJKLine hyline2 = new YJKLine(origin3, origin4);
						XYZ origin5 = hypiping3.StartConnector.Origin;
						XYZ origin6 = hypiping3.EndConnector.Origin;
						YJKLine hyline3 = new YJKLine(origin5, origin6);
						XYZ origin7 = hypiping4.StartConnector.Origin;
						XYZ origin8 = hypiping4.EndConnector.Origin;
						YJKLine hyline4 = new YJKLine(origin7, origin8);
						if (!hyline.IsParallel(hyline2.Line))
						{
							connMSG = "风管2应与风管1平行或在同一直线上。";
							result = familyInstance;
						}
						else if (!hyline3.IsParallel(hyline4.Line))
						{
							connMSG = "风管3应与风管4平行或在同一直线上。";
							result = familyInstance;
						}
						else if (!hyline.IsVertical(hyline3.Line))
						{
							connMSG = "风管3应与风管1垂直。";
							result = familyInstance;
						}
						else
						{
							XYZ dlineP = YJKRevitTools.GetDLineP(origin5, origin6, origin, origin2);
							XYZ dlineP2 = YJKRevitTools.GetDLineP(origin7, origin8, origin, origin2);
							if (dlineP.DistanceTo(dlineP2) > this.GetMoreDiameter(hypiping3, hypiping4) * 2.0)
							{
								connMSG = "风管4位置与风管3间的间距过大，\n不能构成四通连接。";
								result = familyInstance;
							}
							else
							{
								XYZ dlineP3 = YJKRevitTools.GetDLineP(origin, origin2, origin5, origin6);
								XYZ dlineP4 = YJKRevitTools.GetDLineP(origin, origin2, origin7, origin8);
								if (dlineP3.DistanceTo(dlineP4) > this.GetMoreDiameter(hypiping2, hypiping) * 2.0)
								{
									connMSG = "风管1位置与风管2间的间距过大，\n不能构成四通连接。";
									result = familyInstance;
								}
								else
								{
									bool flag = hyline.IsPointOnLine(dlineP);
									bool flag2 = hyline3.IsPointOnLine(dlineP3);
									if (dlineP == null || dlineP3 == null)
									{
										connMSG = "风管3与风管1没有交点，请调整风管位置。";
										result = familyInstance;
									}
									else
									{
										YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
										YJKPiping<Duct> hypiping5;
										if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
										{
											if (!flag)
											{
												connMSG = "风管3的位置不正确，\n风管1与3在风管1上有交点。";
												return familyInstance;
											}
											IList<Duct> list = new List<Duct>();
											list = hypiping.Separate(dlineP);
											double num = xyzs[0].DistanceTo(YJKRevitTools.GetElementCurve(list[1], 0)) + xyzs[0].DistanceTo(YJKRevitTools.GetElementCurve(list[1], 1));
											double num2 = xyzs[1].DistanceTo(YJKRevitTools.GetElementCurve(list[1], 0)) + xyzs[1].DistanceTo(YJKRevitTools.GetElementCurve(list[1], 1));
											if (num > num2)
											{
												hypiping5 = new YJKPiping<Duct>(list[1], hypiping.GlobalPoint);
												hypiping = new YJKPiping<Duct>(list[0], hypiping.GlobalPoint);
											}
											else
											{
												hypiping5 = new YJKPiping<Duct>(list[0], hypiping.GlobalPoint);
												hypiping = new YJKPiping<Duct>(list[1], hypiping.GlobalPoint);
											}
											document.Regenerate();
											hyrotateFamilyInstance.MovePiping(hypiping5.Piping);
											hyrotateFamilyInstance.MovePiping(hypiping.Piping);
										}
										else
										{
											hypiping5 = new YJKPiping<Duct>(hypiping2.Piping, hypiping2.GlobalPoint);
										}
										YJKPiping<Duct> hypiping6;
										if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
										{
											if (!flag2)
											{
												connMSG = "风管1的位置不正确，\n风管1与3在风管3上有交点。";
												return familyInstance;
											}
											IList<Duct> list2 = new List<Duct>();
											list2 = hypiping3.Separate(dlineP3);
											double num3 = xyzs[0].DistanceTo(YJKRevitTools.GetElementCurve(list2[1], 0)) + xyzs[0].DistanceTo(YJKRevitTools.GetElementCurve(list2[1], 1));
											double num2 = xyzs[1].DistanceTo(YJKRevitTools.GetElementCurve(list2[1], 0)) + xyzs[1].DistanceTo(YJKRevitTools.GetElementCurve(list2[1], 1));
											if (num3 > num2)
											{
												hypiping6 = new YJKPiping<Duct>(list2[1], hypiping3.GlobalPoint);
												hypiping3 = new YJKPiping<Duct>(list2[0], hypiping3.GlobalPoint);
											}
											else
											{
												hypiping6 = new YJKPiping<Duct>(list2[0], hypiping3.GlobalPoint);
												hypiping3 = new YJKPiping<Duct>(list2[1], hypiping3.GlobalPoint);
											}
											document.Regenerate();
											hyrotateFamilyInstance.MovePiping(hypiping6.Piping);
											hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
										}
										else
										{
											hypiping6 = new YJKPiping<Duct>(hypiping4.Piping, hypiping4.GlobalPoint);
										}
										Connector closeConnector = hypiping.GetCloseConnector(dlineP, false);
										Connector closeConnector2 = hypiping5.GetCloseConnector(dlineP, false);
										Connector closeConnector3 = hypiping3.GetCloseConnector(dlineP, false);
										Connector closeConnector4 = hypiping6.GetCloseConnector(dlineP, false);
										if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector2.CoordinateSystem.BasisZ, 0.003))
										{
											connMSG = "风管2的位置不正确。";
											result = familyInstance;
										}
										else if (!closeConnector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector4.CoordinateSystem.BasisZ, 0.0005))
										{
											connMSG = "风管4的位置不正确。";
											result = familyInstance;
										}
										else if (listName == null)
										{
											this.NewSomeConnect(new List<Duct>
											{
												hypiping.Piping,
												hypiping5.Piping,
												hypiping3.Piping,
												hypiping6.Piping
											});
											result = null;
										}
										else
										{
											int num4 = hypiping.CheckDuctsRelation(hypiping.Piping, hypiping3.Piping);
											if (num4 == -1)
											{
												connMSG = "没有找到连接风管！";
												result = familyInstance2;
											}
											else
											{
												string text;
												if (num4 == 0)
												{
													text = listName[0];
												}
												else if (num4 == 1)
												{
													text = listName[1];
												}
												else if (num4 == 2)
												{
													text = listName[2];
												}
												else
												{
													text = listName[1];
												}
												string text2 = "";
												ConnectDuctManage connectDuctManage = new ConnectDuctManage();
												FamilySymbol familySymbol = connectDuctManage.GetFamilySymbol(text, document, ref text2);
												if (familySymbol == null)
												{
													if (!string.IsNullOrEmpty(text2))
													{
														connMSG = text2;
														result = familyInstance2;
													}
													else
													{
														connMSG = "加载族失败！";
														result = familyInstance2;
													}
												}
												else
												{
													familySymbol.Activate();
													ConnectHelper connectHelper = new ConnectHelper();
													subTransaction = new SubTransaction(document);
													subTransaction.Start();
													UnifiedModified.ActivateFamilySymbol(familySymbol);
													FamilyInstance familyInstance3 = document.Create.NewFamilyInstance(dlineP, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
													document.Regenerate();
													this.SettingTeeSize(familyInstance3, hypiping.Piping, 1, false);
													this.SettingTeeSize(familyInstance3, hypiping5.Piping, 2, false);
													this.SettingTeeSize(familyInstance3, hypiping3.Piping, 4, false);
													this.SettingTeeSize(familyInstance3, hypiping6.Piping, 5, false);
													Connector connector = connectHelper.GetTeeMainConnector(familyInstance3);
													Connector connector2 = connectHelper.GetTeeOutConnector(familyInstance3, connector.CoordinateSystem.BasisZ);
													Connector connector3 = connectHelper.GetTeeOutConnector(familyInstance3, new XYZ(0.0, -1.0, 0.0));
													Connector connector4 = connectHelper.GetTeeOutConnector(familyInstance3, connector3.CoordinateSystem.BasisZ);
													hyrotateFamilyInstance.MoveFamilyInstance(familyInstance3, connector.CoordinateSystem.BasisZ * 0.05);
													Line line = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(0.0, 0.0, 1.0));
													line.MakeUnbound();
													double num5 = 0.0;
													if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector.CoordinateSystem.BasisZ))
													{
														num5 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-1.0 * connector.CoordinateSystem.BasisZ);
													}
													if (num5 > 0.0001)
													{
														ElementTransformUtils.RotateElement(document, familyInstance3.Id, line, num5);
														document.Regenerate();
														if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisZ))
														{
															ElementTransformUtils.RotateElement(document, familyInstance3.Id, line, Math.PI*2 - 2.0 * num5);
															document.Regenerate();
														}
													}
													int num6 = hypiping.CheckConnLocation(connector3, closeConnector3);
													if (num6 != 1 && num6 != 0)
													{
														IList<Connector> list3 = this.MirrorCrossFitting(ref familyInstance3, connector, connector2, connector3, connector4);
														if (list3.Count <= 0)
														{
															connMSG = "镜像后三通没有找到！";
															return familyInstance2;
														}
														connector2 = list3[1];
														connector = list3[0];
														connector3 = list3[2];
														connector4 = list3[3];
													}
													num6 = hypiping.CheckConnLocation(connector3, closeConnector3);
													if (num6 == -1)
													{
														connMSG = "风管3位置不正确！";
														result = familyInstance2;
													}
													else if (num6 == -2)
													{
														connMSG = "找不到连接器！";
														result = familyInstance2;
													}
													else
													{
														num6 = hypiping.CheckConnLocation(connector4, closeConnector4);
														if (num6 == -1)
														{
															connMSG = "风管4位置不正确！";
															result = familyInstance2;
														}
														else if (num6 == -2)
														{
															connMSG = "找不到连接器！";
															result = familyInstance2;
														}
														else
														{
															Duct elem = null;
															closeConnector.ConnectTo(connector);
															connectDuctManage.SetDuctReduceFitting(new List<Duct>
															{
																hypiping.Piping,
																hypiping3.Piping,
																hypiping5.Piping,
																hypiping6.Piping
															});
															if (hypiping.CheckConnLocation(connector2, closeConnector2) == 1)
															{
																if (this.IsSameSizeConn(connector2, closeConnector2))
																{
																	connector2.ConnectTo(closeConnector2);
																}
																else
																{
																	Connector connector5;
																	if (text.Contains("插管"))
																	{
																		connector5 = this.CreatLittlePipe(hypiping.Piping, connector2, ref elem);
																	}
																	else
																	{
																		connector5 = this.CreatLittlePipe(hypiping5.Piping, connector2, ref elem);
																	}
																	FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector2);
																	document.DeleteElement(elem);
																	document.Regenerate();
																	Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector2);
																	Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																	if (notConnectedConn != null && notConnectedConn2 != null)
																	{
																		notConnectedConn.ConnectTo(notConnectedConn2);
																	}
																}
															}
															else
															{
																Connector connector5;
																if (text.Contains("插管"))
																{
																	connector5 = this.CreatLittlePipe(hypiping.Piping, connector2, ref elem);
																}
																else
																{
																	connector5 = this.CreatLittlePipe(hypiping5.Piping, connector2, ref elem);
																}
																FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector2);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector2);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															if (hypiping.CheckConnLocation(connector3, closeConnector3) == 1)
															{
																if (this.IsSameSizeConn(connector3, closeConnector3))
																{
																	connector3.ConnectTo(closeConnector3);
																}
																else
																{
																	Connector connector5 = this.CreatLittlePipe(hypiping3.Piping, connector3, ref elem);
																	FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector3);
																	document.DeleteElement(elem);
																	document.Regenerate();
																	Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector3);
																	Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																	if (notConnectedConn != null && notConnectedConn2 != null)
																	{
																		notConnectedConn.ConnectTo(notConnectedConn2);
																	}
																}
															}
															else
															{
																Connector connector5 = this.CreatLittlePipe(hypiping3.Piping, connector3, ref elem);
																FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector3);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector3);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															if (hypiping.CheckConnLocation(connector4, closeConnector4) == 1)
															{
																if (this.IsSameSizeConn(connector4, closeConnector4))
																{
																	connector4.ConnectTo(closeConnector4);
																}
																else
																{
																	Connector connector5 = this.CreatLittlePipe(hypiping6.Piping, connector4, ref elem);
																	FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector4);
																	document.DeleteElement(elem);
																	document.Regenerate();
																	Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector4);
																	Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector4);
																	if (notConnectedConn != null && notConnectedConn2 != null)
																	{
																		notConnectedConn.ConnectTo(notConnectedConn2);
																	}
																}
															}
															else
															{
																Connector connector5 = this.CreatLittlePipe(hypiping6.Piping, connector4, ref elem);
																FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector4);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector4);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector4);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															document.Regenerate();
															this.SettingTeeSize(familyInstance3, this.GetMaxDuctSize(hypiping6.Piping, hypiping3.Piping));
															double length = hypiping3.GetDiameter() / 2.0;
															double length2 = hypiping.GetDiameter() / 2.0;
															this.SettingFittingLength(familyInstance3, length, 1);
															this.SettingFittingLength(familyInstance3, length, 2);
															this.SettingFittingLength(familyInstance3, length2, 4);
															this.SettingFittingLength(familyInstance3, length2, 5);
															hyrotateFamilyInstance.MovePiping(hypiping.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping5.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping6.Piping);
															subTransaction.Commit();
															result = familyInstance3;
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		private double GetMoreDiameter(YJKPiping<Duct> myDuct1, YJKPiping<Duct> myDuct2)
		{
			if (myDuct1.GetDiameter() <= myDuct2.GetDiameter())
			{
				return myDuct2.GetDiameter();
			}
			return myDuct1.GetDiameter();
		}

		public FamilyInstance NewCrossFittingY(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMSG)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance2 = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 4)
				{
					connMSG = "请选择四根风管，才可以进行四通连接！";
					result = familyInstance;
				}
				else
				{
					Autodesk.Revit.DB.Document document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[3], xyzs[3]);
					if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id) && hypiping.Piping.Id.Equals(hypiping4.Piping.Id))
					{
						connMSG = "同一根风管不能构成四通连接！";
						result = familyInstance2;
					}
					else if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id) || hypiping.Piping.Id.Equals(hypiping4.Piping.Id))
					{
						connMSG = "第一根风管与第三根或第四根风管不能为同一风管！";
						result = familyInstance2;
					}
					else if (hypiping2.Piping.Id.Equals(hypiping3.Piping.Id) || hypiping2.Piping.Id.Equals(hypiping4.Piping.Id))
					{
						connMSG = "第二根风管与第三根或第四根风管不能为同一风管！";
						result = familyInstance2;
					}
					else
					{
						XYZ origin = hypiping.StartConnector.Origin;
						XYZ origin2 = hypiping.EndConnector.Origin;
						YJKLine hyline = new YJKLine(origin, origin2);
						XYZ origin3 = hypiping2.StartConnector.Origin;
						XYZ origin4 = hypiping2.EndConnector.Origin;
						YJKLine hyline2 = new YJKLine(origin3, origin4);
						XYZ origin5 = hypiping3.StartConnector.Origin;
						XYZ origin6 = hypiping3.EndConnector.Origin;
						YJKLine hyline3 = new YJKLine(origin5, origin6);
						XYZ origin7 = hypiping4.StartConnector.Origin;
						XYZ origin8 = hypiping4.EndConnector.Origin;
						new YJKLine(origin7, origin8);
						if (!hyline.IsParallel(hyline2.Line))
						{
							connMSG = "风管2应与风管1平行或在同一直线上。";
							result = familyInstance;
						}
						else
						{
							XYZ dlineP = YJKRevitTools.GetDLineP(origin5, origin6, origin, origin2);
							XYZ dlineP2 = YJKRevitTools.GetDLineP(origin, origin2, origin5, origin6);
							bool flag = hyline.IsPointOnLine(dlineP);
							hyline3.IsPointOnLine(dlineP2);
							if (dlineP == null || dlineP2 == null)
							{
								connMSG = "风管3与风管1没有交点，请调整风管位置。";
								result = familyInstance;
							}
							else
							{
								YJKPiping<Duct> hypiping5;
								if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
								{
									if (!flag)
									{
										connMSG = "风管3的位置不正确，\n风管1与3在风管1上有交点。";
										return familyInstance;
									}
									IList<Duct> list = new List<Duct>();
									list = hypiping2.Separate(dlineP);
									double num = xyzs[0].DistanceTo(YJKRevitTools.GetElementCurve(list[1], 0)) + xyzs[0].DistanceTo(YJKRevitTools.GetElementCurve(list[1], 1));
									double num2 = xyzs[1].DistanceTo(YJKRevitTools.GetElementCurve(list[1], 0)) + xyzs[1].DistanceTo(YJKRevitTools.GetElementCurve(list[1], 1));
									if (num > num2)
									{
										hypiping5 = new YJKPiping<Duct>(list[1], hypiping.GlobalPoint);
										hypiping = new YJKPiping<Duct>(list[0], hypiping.GlobalPoint);
									}
									else
									{
										hypiping5 = new YJKPiping<Duct>(list[0], hypiping.GlobalPoint);
										hypiping = new YJKPiping<Duct>(list[1], hypiping.GlobalPoint);
									}
									document.Regenerate();
								}
								else
								{
									hypiping5 = new YJKPiping<Duct>(hypiping2.Piping, hypiping2.GlobalPoint);
								}
								if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
								{
									connMSG = "风管3与4为同一根风管，无法构成斜四通连接关系。";
									result = familyInstance;
								}
								else
								{
									YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(hypiping4.Piping, hypiping4.GlobalPoint);
									Connector closeConnector = hypiping.GetCloseConnector(dlineP, false);
									Connector closeConnector2 = hypiping5.GetCloseConnector(dlineP, false);
									Connector closeConnector3 = hypiping3.GetCloseConnector(dlineP, false);
									Connector closeConnector4 = hypiping6.GetCloseConnector(dlineP, false);
									if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector2.CoordinateSystem.BasisZ, 0.0005))
									{
										connMSG = "风管2的位置不正确。";
										result = familyInstance;
									}
									else
									{
										double num3 = closeConnector.CoordinateSystem.BasisZ.AngleTo(closeConnector3.CoordinateSystem.BasisZ);
										double num4 = closeConnector.CoordinateSystem.BasisZ.AngleTo(closeConnector4.CoordinateSystem.BasisZ);
										if (num3 < Math.PI*.5 || num3 > Math.PI)
										{
											connMSG = "风管3的位置不正确,其与风管1的夹角应该在90°-180°";
											result = familyInstance;
										}
										else if (num4 < Math.PI*.5 || num4 > Math.PI)
										{
											connMSG = "风管4的位置不正确,其与风管1的夹角应该在90°-180°";
											result = familyInstance;
										}
										else if (listName == null)
										{
											this.NewSomeConnect(new List<Duct>
											{
												hypiping.Piping,
												hypiping5.Piping,
												hypiping3.Piping,
												hypiping6.Piping
											});
											result = null;
										}
										else
										{
											int num5 = hypiping.CheckDuctsRelation(hypiping.Piping, hypiping3.Piping);
											if (num5 == -1)
											{
												connMSG = "没有找到连接风管！";
												result = familyInstance2;
											}
											else
											{
												string text;
												if (num5 == 0)
												{
													text = listName[0];
												}
												else if (num5 == 1)
												{
													text = listName[1];
												}
												else if (num5 == 2)
												{
													text = listName[2];
												}
												else
												{
													text = listName[1];
												}
												string text2 = "";
												ConnectDuctManage connectDuctManage = new ConnectDuctManage();
												FamilySymbol familySymbol = connectDuctManage.GetFamilySymbol(text, document, ref text2);
												if (familySymbol == null)
												{
													if (!string.IsNullOrEmpty(text2))
													{
														connMSG = text2;
														result = familyInstance2;
													}
													else
													{
														connMSG = "加载族失败！";
														result = familyInstance2;
													}
												}
												else
												{
													ConnectHelper connectHelper = new ConnectHelper();
													subTransaction = new SubTransaction(document);
													subTransaction.Start();
													YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
													UnifiedModified.ActivateFamilySymbol(familySymbol);
													FamilyInstance familyInstance3 = document.Create.NewFamilyInstance(dlineP, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
													document.Regenerate();
													this.SettingTeeSize(familyInstance3, hypiping.Piping, 1, false);
													this.SettingTeeSize(familyInstance3, hypiping5.Piping, 2, false);
													this.SettingTeeSize(familyInstance3, hypiping3.Piping, 4, false);
													this.SettingTeeSize(familyInstance3, hypiping6.Piping, 5, false);
													Connector connector = connectHelper.GetTeeMainConnector(familyInstance3);
													Connector connector2 = connectHelper.GetTeeOutConnector(familyInstance3, connector.CoordinateSystem.BasisZ);
													Connector connector3 = connectHelper.GetTeeOutConnector(familyInstance3, -1.0 * new XYZ(Math.Sqrt(2.0) / 2.0, Math.Sqrt(2.0) / 2.0, 0.0));
													Connector connector4 = connectHelper.GetTeeOutConnector(familyInstance3, -1.0 * new XYZ(Math.Sqrt(2.0) / 2.0, -1.0 * Math.Sqrt(2.0) / 2.0, 0.0));
													this.SettingTeeAngle(familyInstance3, Math.PI - num3, 3);
													this.SettingTeeAngle(familyInstance3, Math.PI - num4, 4);
													hyrotateFamilyInstance.MoveFamilyInstance(familyInstance3, connector.CoordinateSystem.BasisZ * 0.05);
													Line line = YJKLineEx.YJKGetBound(dlineP, dlineP + new XYZ(0.0, 0.0, 1.0));
													line.MakeUnbound();
													double num6 = 0.0;
													if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * closeConnector.CoordinateSystem.BasisZ))
													{
														num6 = closeConnector.CoordinateSystem.BasisZ.AngleTo(-1.0 * connector.CoordinateSystem.BasisZ);
													}
													if (num6 > 0.0001)
													{
														ElementTransformUtils.RotateElement(document, familyInstance3.Id, line, num6);
														document.Regenerate();
														if (!closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisZ))
														{
															ElementTransformUtils.RotateElement(document, familyInstance3.Id, line, Math.PI*2 - 2.0 * num6);
															document.Regenerate();
														}
													}
													int num7 = hypiping.CheckConnLocation(connector3, closeConnector3);
													if (num7 != 1 && num7 != 0)
													{
														IList<Connector> list2 = this.MirrorCrossFitting(ref familyInstance3, connector, connector2, closeConnector3, closeConnector4);
														if (list2.Count <= 0)
														{
															connMSG = "镜像后三通没有找到！";
															return familyInstance2;
														}
														connector2 = list2[1];
														connector = list2[0];
														connector3 = list2[2];
														connector4 = list2[3];
													}
													num7 = hypiping.CheckConnLocation(connector3, closeConnector3);
													if (num7 == -1)
													{
														connMSG = "风管3位置不正确！";
														result = familyInstance2;
													}
													else if (num7 == -2)
													{
														connMSG = "找不到连接器！";
														result = familyInstance2;
													}
													else
													{
														num7 = hypiping.CheckConnLocation(connector4, closeConnector4);
														if (num7 == -1)
														{
															connMSG = "风管4位置不正确！";
															result = familyInstance2;
														}
														else if (num7 == -2)
														{
															connMSG = "找不到连接器！";
															result = familyInstance2;
														}
														else
														{
															Duct elem = null;
															closeConnector.ConnectTo(connector);
															connectDuctManage.SetDuctReduceFitting(new List<Duct>
															{
																hypiping.Piping,
																hypiping3.Piping,
																hypiping5.Piping,
																hypiping6.Piping
															});
															if (hypiping.CheckConnLocation(connector2, closeConnector2) == 1)
															{
																if (this.IsSameSizeConn(connector2, closeConnector2))
																{
																	connector2.ConnectTo(closeConnector2);
																}
																else
																{
																	Connector connector5;
																	if (text.Contains("插管"))
																	{
																		connector5 = this.CreatLittlePipe(hypiping.Piping, connector2, ref elem);
																	}
																	else
																	{
																		connector5 = this.CreatLittlePipe(hypiping5.Piping, connector2, ref elem);
																	}
																	FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector2);
																	document.DeleteElement(elem);
																	document.Regenerate();
																	Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector2);
																	Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																	if (notConnectedConn != null && notConnectedConn2 != null)
																	{
																		notConnectedConn.ConnectTo(notConnectedConn2);
																	}
																}
															}
															else
															{
																Connector connector5;
																if (text.Contains("插管"))
																{
																	connector5 = this.CreatLittlePipe(hypiping.Piping, connector2, ref elem);
																}
																else
																{
																	connector5 = this.CreatLittlePipe(hypiping5.Piping, connector2, ref elem);
																}
																FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector2);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector2);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector2);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															if (hypiping.CheckConnLocation(connector3, closeConnector3) == 1)
															{
																if (this.IsSameSizeConn(connector3, closeConnector3))
																{
																	connector3.ConnectTo(closeConnector3);
																}
																else
																{
																	Connector connector5 = this.CreatLittlePipe(hypiping3.Piping, connector3, ref elem);
																	FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector3);
																	document.DeleteElement(elem);
																	document.Regenerate();
																	Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector3);
																	Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																	if (notConnectedConn != null && notConnectedConn2 != null)
																	{
																		notConnectedConn.ConnectTo(notConnectedConn2);
																	}
																}
															}
															else
															{
																Connector connector5 = this.CreatLittlePipe(hypiping3.Piping, connector3, ref elem);
																FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector3);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector3);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector3);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															if (hypiping.CheckConnLocation(connector4, closeConnector4) == 1)
															{
																if (this.IsSameSizeConn(connector4, closeConnector4))
																{
																	connector4.ConnectTo(closeConnector4);
																}
																else
																{
																	Connector connector5 = this.CreatLittlePipe(hypiping6.Piping, connector4, ref elem);
																	FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector4);
																	document.DeleteElement(elem);
																	document.Regenerate();
																	Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector4);
																	Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector4);
																	if (notConnectedConn != null && notConnectedConn2 != null)
																	{
																		notConnectedConn.ConnectTo(notConnectedConn2);
																	}
																}
															}
															else
															{
																Connector connector5 = this.CreatLittlePipe(hypiping6.Piping, connector4, ref elem);
																FamilyInstance fi = document.Create.NewTransitionFitting(connector5, closeConnector4);
																document.DeleteElement(elem);
																document.Regenerate();
																Connector notConnectedConn = this.GetNotConnectedConn(familyInstance3, closeConnector4);
																Connector notConnectedConn2 = this.GetNotConnectedConn(fi, connector4);
																if (notConnectedConn != null && notConnectedConn2 != null)
																{
																	notConnectedConn.ConnectTo(notConnectedConn2);
																}
															}
															document.Regenerate();
															this.SettingTeeSize(familyInstance3, this.GetMaxDuctSize(hypiping6.Piping, hypiping3.Piping));
															double length = hypiping3.GetDiameter() / 2.0;
															double length2 = hypiping.GetDiameter() / 2.0;
															this.SettingFittingLength(familyInstance3, length, 1);
															this.SettingFittingLength(familyInstance3, length, 2);
															this.SettingFittingLength(familyInstance3, length2, 4);
															this.SettingFittingLength(familyInstance3, length2, 5);
															hyrotateFamilyInstance.MovePiping(hypiping.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping5.Piping);
															hyrotateFamilyInstance.MovePiping(hypiping6.Piping);
															subTransaction.Commit();
															result = familyInstance3;
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		private Connector CreatLittlePipe(Duct duct, Connector conn, ref Duct newDuct)
		{
			XYZ origin = conn.Origin;
			XYZ basisZ = conn.CoordinateSystem.BasisZ;
			Duct duct2 = YJKRevitTools.NewDuct(duct, origin, origin + 0.016404199475065617 * basisZ);
			duct.Document.Regenerate();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct2);
			new YJKRotateFamilyInstance();
			Connector connector;
			Connector connector2;
			if (conn.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping.StartConnector.Connector.CoordinateSystem.BasisZ, 0.0001))
			{
				connector = hypiping.EndConnector.Connector;
				connector2 = hypiping.StartConnector.Connector;
			}
			else
			{
				connector2 = hypiping.EndConnector.Connector;
				connector = hypiping.StartConnector.Connector;
			}
			connector.ConnectTo(conn);
			newDuct = duct2;
			return connector2;
		}

		private Connector GetNotConnectedConn(FamilyInstance fi, Connector connector)
		{
			Connector result = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector2 = obj as Connector;
					if (connector2 != null && !connector2.IsConnected && connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * connector.CoordinateSystem.BasisZ, 0.0005))
					{
						result = connector2;
					}
				}
			}
			return result;
		}

		private FamilySymbol GetFamilySymbol(string fsName, Autodesk.Revit.DB.Document doc, ref string loadMSG)
		{
			FamilySymbol familySymbol = null;
			if (string.IsNullOrEmpty(fsName))
			{
				loadMSG = "族名不能为空！";
				return familySymbol;
			}
			familySymbol = YJKRevitTools.GetFamilySymbol(fsName, doc);
			if (familySymbol == null)
			{
				string text = "C:\\Users\\zhang\\Desktop\\" + fsName + ".rfa";
				if (!File.Exists(text))
				{
					loadMSG = "族不存在或者加载地址不正确！";
					return familySymbol;
				}
				SubTransaction subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				Family family = null;
				doc.LoadFamily(text, out family);
				if (family == null)
				{
					subTransaction.RollBack();
					return null;
				}
				familySymbol = YJKRevitTools.GetFamilySymbol(fsName, doc);
				subTransaction.Commit();
			}
			return familySymbol;
		}

		private bool IsSameConnector(Connector conn1, Connector conn2)
		{
			bool result = true;
			if (conn1 == null || conn2 == null)
			{
				return false;
			}
			int num = 0;
			if (conn1.Shape == ConnectorSharp.Rect)
			{
				num = 1;
			}
			else if (conn1.Shape == ConnectorSharp.Round)
			{
				num = 2;
			}
			else if (conn1.Shape == ConnectorSharp.Oval)
			{
				num = 3;
			}
			else if (conn1.Shape == ConnectorSharp.Invalid)
			{
				num = 0;
			}
			double num2 = conn1.CoordinateSystem.BasisZ.AngleTo(-conn2.CoordinateSystem.BasisZ);
			if (!conn1.CoordinateSystem.BasisZ.IsAlmostEqualTo(-conn2.CoordinateSystem.BasisZ) || num2 > 0.0001)
			{
				result = false;
			}
			else if (conn1.Shape != conn2.Shape)
			{
				result = false;
			}
			else if (1 == num || 3 == num)
			{
				if (Math.Abs(conn1.Width - conn2.Width) > 0.0001 || Math.Abs(conn1.Height - conn2.Height) > 0.0001)
				{
					result = false;
				}
			}
			else if (2 == num)
			{
				if (Math.Abs(conn1.Radius - conn2.Radius) > 0.0001)
				{
					result = false;
				}
			}
			else if (num == 0)
			{
				throw new Exception("接头未初始化");
			}
			return result;
		}

		private bool IsSameSizeConn(Connector conn1, Connector conn2)
		{
			bool result = true;
			if (conn1 == null || conn2 == null)
			{
				return false;
			}
			int num = 0;
			if (conn1.Shape == ConnectorSharp.Rect)
			{
				num = 1;
			}
			else if (conn1.Shape == ConnectorSharp.Round)
			{
				num = 2;
			}
			else if (conn1.Shape == ConnectorSharp.Oval)
			{
				num = 3;
			}
			else if (conn1.Shape == ConnectorSharp.Invalid)
			{
				num = 0;
			}
			if (conn1.Shape != conn2.Shape)
			{
				result = false;
			}
			else if (1 == num || 3 == num)
			{
				if (Math.Abs(conn1.Width - conn2.Width) > 0.0001 || Math.Abs(conn1.Height - conn2.Height) > 0.0001)
				{
					result = false;
				}
			}
			else if (2 == num)
			{
				if (Math.Abs(conn1.Radius - conn2.Radius) > 0.0001)
				{
					result = false;
				}
			}
			else if (num == 0)
			{
				throw new Exception("接头未初始化");
			}
			return result;
		}

		private int SettingTeeSize(FamilyInstance fi, Duct duct, int orderPipe, bool IsConnRiser = false)
		{
			if (fi == null || duct == null)
			{
				return -1;
			}
			Parameter parameter = duct.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM);
			Parameter parameter2;
			Parameter parameter3;
			if (IsConnRiser)
			{
				parameter2 = duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
				parameter3 = duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
			}
			else
			{
				parameter2 = duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
				parameter3 = duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
			}
			bool flag = false;
			if (parameter != null && !parameter.IsReadOnly && parameter.HasValue)
			{
				switch (orderPipe)
				{
				case 1:
				{
					Parameter parameter4 = fi.GetParameter("风管直径1");
					if (parameter4 != null && !parameter4.IsReadOnly)
					{
						flag = parameter4.Set(parameter.AsDouble());
					}
					break;
				}
				case 2:
				{
					Parameter parameter4 = fi.GetParameter("风管直径2");
					if (parameter4 != null && !parameter4.IsReadOnly)
					{
						flag = parameter4.Set(parameter.AsDouble());
					}
					break;
				}
				case 3:
				{
					Parameter parameter4 = fi.GetParameter("风管直径3");
					if (parameter4 != null && !parameter4.IsReadOnly)
					{
						flag = parameter4.Set(parameter.AsDouble());
					}
					break;
				}
				case 4:
				{
					Parameter parameter4 = fi.GetParameter("风管直径3");
					if (parameter4 != null && !parameter4.IsReadOnly)
					{
						flag = parameter4.Set(parameter.AsDouble());
					}
					break;
				}
				case 5:
				{
					Parameter parameter4 = fi.GetParameter("风管直径4");
					if (parameter4 != null && !parameter4.IsReadOnly)
					{
						flag = parameter4.Set(parameter.AsDouble());
					}
					break;
				}
				}
				if (flag)
				{
					return 0;
				}
			}
			if (parameter2 != null && !parameter2.IsReadOnly && parameter2.HasValue)
			{
				switch (orderPipe)
				{
				case 1:
				{
					Parameter parameter5 = fi.GetParameter("风管宽度1");
					if (parameter5 != null && !parameter5.IsReadOnly)
					{
						flag = parameter5.Set(parameter2.AsDouble());
					}
					break;
				}
				case 2:
				{
					Parameter parameter5 = fi.GetParameter("风管宽度2");
					if (parameter5 != null && !parameter5.IsReadOnly)
					{
						flag = parameter5.Set(parameter2.AsDouble());
					}
					break;
				}
				case 3:
				{
					Parameter parameter5 = fi.GetParameter("风管宽度3");
					if (parameter5 != null && !parameter5.IsReadOnly)
					{
						flag = parameter5.Set(parameter2.AsDouble());
					}
					break;
				}
				case 4:
				{
					Parameter parameter5 = fi.GetParameter("风管宽度3");
					if (parameter5 != null && !parameter5.IsReadOnly)
					{
						flag = parameter5.Set(parameter2.AsDouble());
					}
					break;
				}
				case 5:
				{
					Parameter parameter5 = fi.GetParameter("风管宽度4");
					if (parameter5 != null && !parameter5.IsReadOnly)
					{
						flag = parameter5.Set(parameter2.AsDouble());
					}
					break;
				}
				}
			}
			if (parameter3 == null || parameter3.IsReadOnly || !parameter3.HasValue || !flag)
			{
				return -3;
			}
			switch (orderPipe)
			{
			case 1:
			{
				Parameter parameter6 = fi.GetParameter("风管高度1");
				if (parameter6 != null && !parameter6.IsReadOnly)
				{
					flag = parameter6.Set(parameter3.AsDouble());
				}
				break;
			}
			case 2:
			{
				Parameter parameter6 = fi.GetParameter("风管高度2");
				if (parameter6 != null && !parameter6.IsReadOnly)
				{
					flag = parameter6.Set(parameter3.AsDouble());
				}
				break;
			}
			case 3:
			{
				Parameter parameter6 = fi.GetParameter("风管高度3");
				if (parameter6 != null && !parameter6.IsReadOnly)
				{
					flag = parameter6.Set(parameter3.AsDouble());
				}
				break;
			}
			case 4:
			{
				Parameter parameter6 = fi.GetParameter("风管高度3");
				if (parameter6 != null && !parameter6.IsReadOnly)
				{
					flag = parameter6.Set(parameter3.AsDouble());
				}
				break;
			}
			case 5:
			{
				Parameter parameter6 = fi.GetParameter("风管高度4");
				if (parameter6 != null && !parameter6.IsReadOnly)
				{
					flag = parameter6.Set(parameter3.AsDouble());
				}
				break;
			}
			}
			if (flag)
			{
				return 1;
			}
			return -2;
		}

		private int SettingTeeAngle(FamilyInstance fi, double angle, int ductNum)
		{
			if (fi == null || angle <= 0.0)
			{
				return -1;
			}
			Parameter parameter = null;
			switch (ductNum)
			{
			case 1:
				parameter = fi.GetParameter("出口角度");
				break;
			case 2:
				parameter = fi.GetParameter("支管角度");
				break;
			case 3:
				parameter = fi.GetParameter("支管1角度");
				break;
			case 4:
				parameter = fi.GetParameter("支管2角度");
				break;
			case 5:
				parameter = fi.GetParameter("风管2角度");
				break;
			case 6:
				parameter = fi.GetParameter("风管3角度");
				break;
			}
			bool flag = false;
			if (parameter != null && !parameter.IsReadOnly)
			{
				flag = parameter.Set(angle);
			}
			if (flag)
			{
				return 0;
			}
			return -2;
		}

		private int SettingFittingLength(FamilyInstance fi, double length, int ductNum)
		{
			if (fi == null || length <= 0.0)
			{
				return -1;
			}
			Parameter parameter = null;
			switch (ductNum)
			{
			case 1:
				parameter = fi.GetParameter("入口长度");
				break;
			case 2:
				parameter = fi.GetParameter("出口长度");
				break;
			case 3:
				parameter = fi.GetParameter("支管长度");
				break;
			case 4:
				parameter = fi.GetParameter("支管1长度");
				break;
			case 5:
				parameter = fi.GetParameter("支管2长度");
				break;
			}
			bool flag = false;
			if (parameter != null && !parameter.IsReadOnly)
			{
				flag = parameter.Set(length);
			}
			if (flag)
			{
				return 0;
			}
			return -2;
		}

		private double GetMaxDuctSize(Duct duct1, Duct duct2)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct1);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
			double weightOrDiameter = hypiping.GetWeightOrDiameter();
			double heightOrDiameter = hypiping.GetHeightOrDiameter();
			double weightOrDiameter2 = hypiping2.GetWeightOrDiameter();
			double heightOrDiameter2 = hypiping2.GetHeightOrDiameter();
			double num = (weightOrDiameter >= weightOrDiameter2) ? weightOrDiameter : weightOrDiameter2;
			double num2 = (heightOrDiameter >= heightOrDiameter2) ? heightOrDiameter : heightOrDiameter2;
			if (num < num2)
			{
				return num2;
			}
			return num;
		}

		private int SettingTeeSize(FamilyInstance fi, double radius)
		{
			if (fi == null || radius <= 0.0)
			{
				return -1;
			}
			Parameter parameter = fi.GetParameter("中心半径");
			bool flag = false;
			if (parameter != null && !parameter.IsReadOnly)
			{
				flag = parameter.Set(radius);
			}
			if (flag)
			{
				return 0;
			}
			return -2;
		}

		private void SettingTeeParameter(FamilyInstance teeFi, YJKPiping<Duct> hyDuct, YJKPiping<Duct> hyDuctOther, int i)
		{
			IList<Connector> appropriateConnector = hyDuct.GetAppropriateConnector(hyDuctOther.Piping);
			double num = 0.0;
			if (appropriateConnector != null && appropriateConnector.Count > 1)
			{
				if (appropriateConnector[0].CoordinateSystem.BasisZ.IsAlmostEqualTo(-appropriateConnector[1].CoordinateSystem.BasisZ))
				{
					num = 0.0;
				}
				else
				{
					num = appropriateConnector[0].CoordinateSystem.BasisZ.AngleTo(-appropriateConnector[1].CoordinateSystem.BasisZ);
				}
			}
			Parameter parameter = hyDuctOther.Piping.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
			Parameter parameter2 = hyDuctOther.Piping.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
			Parameter parameter3 = hyDuctOther.Piping.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM);
			if (1 == i)
			{
				if (parameter3 != null)
				{
					Parameter parameter4 = teeFi.GetParameter("风管直径2");
					if (parameter4 != null && !parameter4.IsReadOnly)
					{
						parameter4.Set(parameter3.AsDouble());
					}
				}
				else
				{
					Parameter parameter5 = teeFi.GetParameter("风管宽度2");
					if (parameter5 != null && !parameter5.IsReadOnly)
					{
						parameter5.Set(parameter.AsDouble());
					}
					Parameter parameter6 = teeFi.GetParameter("风管高度2");
					if (parameter6 != null && !parameter6.IsReadOnly)
					{
						parameter6.Set(parameter2.AsDouble());
					}
				}
				if (num > 0.0001)
				{
					Parameter parameter7 = teeFi.GetParameter("出口角度");
					if (parameter7 != null && !parameter7.IsReadOnly)
					{
						parameter7.Set(num);
						return;
					}
					if (Math.Abs(num - Math.PI*.5) > 0.0001)
					{
						throw new Exception("管件没有出口角度或角度为只读，请选择正确的管件");
					}
				}
			}
			else if (2 == i)
			{
				if (parameter3 != null)
				{
					Parameter parameter8 = teeFi.GetParameter("风管直径3");
					if (parameter8 != null && !parameter8.IsReadOnly)
					{
						parameter8.Set(parameter3.AsDouble());
					}
				}
				else
				{
					Parameter parameter9 = teeFi.GetParameter("风管宽度3");
					if (parameter9 != null && !parameter9.IsReadOnly)
					{
						parameter9.Set(parameter.AsDouble());
					}
					Parameter parameter10 = teeFi.GetParameter("风管高度3");
					if (parameter10 != null && !parameter10.IsReadOnly)
					{
						parameter10.Set(parameter2.AsDouble());
					}
				}
				if (num > 0.0001)
				{
					Parameter parameter11 = teeFi.GetParameter("支管角度");
					if (parameter11 != null && !parameter11.IsReadOnly)
					{
						parameter11.Set(num);
						return;
					}
					if (Math.Abs(num - Math.PI*.5) > 0.0001)
					{
						throw new Exception("管件没有支管角度或角度为只读，请选择正确的管件");
					}
				}
			}
		}

		public bool NewCrossFitting(IList<Duct> ducts, XYZ direction = null)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 4)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				new List<Duct>();
				IList<Duct> list = this.OrderDuctPosition(ducts);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list[1]);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list[2]);
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(list[3]);
				YJKPiping<Duct> hypiping5 = null;
				YJKPiping<Duct> hypiping6 = null;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping);
				if (xyz != null)
				{
					if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
					{
						new List<Duct>();
						hypiping5 = new YJKPiping<Duct>(((IList<Duct>)hypiping.Separate(xyz))[1]);
					}
					else if (hypiping.IsPipingOnLine(hypiping2.Piping))
					{
						hypiping5 = new YJKPiping<Duct>(hypiping2.Piping);
					}
					if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
					{
						new List<Duct>();
						hypiping6 = new YJKPiping<Duct>(((IList<Duct>)hypiping3.Separate(xyz))[1]);
					}
					else if (hypiping3.IsPipingOnLine(hypiping4.Piping))
					{
						hypiping6 = new YJKPiping<Duct>(hypiping4.Piping);
					}
					if (hypiping5 != null && hypiping6 != null)
					{
						Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
						Connector appropriateConnector2 = hypiping5.GetAppropriateConnector(xyz);
						Connector appropriateConnector3 = hypiping3.GetAppropriateConnector(xyz);
						Connector appropriateConnector4 = hypiping6.GetAppropriateConnector(xyz);
						if (appropriateConnector != null && !appropriateConnector.IsConnected && appropriateConnector2 != null && !appropriateConnector2.IsConnected && appropriateConnector3 != null && !appropriateConnector3.IsConnected && appropriateConnector4 != null && !appropriateConnector4.IsConnected)
						{
							if (direction != null)
							{
								List<Connector> crossCon = new List<Connector>
								{
									appropriateConnector,
									appropriateConnector2,
									appropriateConnector3,
									appropriateConnector4
								};
								List<Connector> list2 = this.OrderCrossConnector(crossCon, direction);
								document.Create.NewCrossFitting(list2[0], list2[1], list2[2], list2[3]);
								flag = true;
							}
							else
							{
								FamilyInstance fi = document.Create.NewCrossFitting(appropriateConnector, appropriateConnector2, appropriateConnector3, appropriateConnector4);
								this.ResizeFitting(fi);
								flag = true;
							}
						}
					}
				}
				document.Regenerate();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败！");
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool NewCrossFitting(ref IList<Duct> ducts, XYZ direction = null)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 4)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				new List<Duct>();
				IList<Duct> list = this.OrderDuctPosition(ducts);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list[1]);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list[2]);
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(list[3]);
				YJKPiping<Duct> hypiping5 = null;
				YJKPiping<Duct> hypiping6 = null;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping);
				if (xyz != null)
				{
					if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
					{
						new List<Duct>();
						hypiping5 = new YJKPiping<Duct>(((IList<Duct>)hypiping.Separate(xyz))[1]);
					}
					else if (hypiping.IsPipingOnLine(hypiping2.Piping))
					{
						hypiping5 = new YJKPiping<Duct>(hypiping2.Piping);
					}
					if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
					{
						new List<Duct>();
						hypiping6 = new YJKPiping<Duct>(((IList<Duct>)hypiping3.Separate(xyz))[1]);
					}
					else if (hypiping3.IsPipingOnLine(hypiping4.Piping))
					{
						hypiping6 = new YJKPiping<Duct>(hypiping4.Piping);
					}
					if (hypiping5 != null && hypiping6 != null)
					{
						Connector appropriateConnector = hypiping.GetAppropriateConnector(xyz);
						Connector appropriateConnector2 = hypiping5.GetAppropriateConnector(xyz);
						Connector appropriateConnector3 = hypiping3.GetAppropriateConnector(xyz);
						Connector appropriateConnector4 = hypiping6.GetAppropriateConnector(xyz);
						if (appropriateConnector != null && !appropriateConnector.IsConnected && appropriateConnector2 != null && !appropriateConnector2.IsConnected && appropriateConnector3 != null && !appropriateConnector3.IsConnected && appropriateConnector4 != null && !appropriateConnector4.IsConnected)
						{
							if (direction != null)
							{
								List<Connector> crossCon = new List<Connector>
								{
									appropriateConnector,
									appropriateConnector2,
									appropriateConnector3,
									appropriateConnector4
								};
								List<Connector> list2 = this.OrderCrossConnector(crossCon, direction);
								document.Create.NewCrossFitting(list2[0], list2[1], list2[2], list2[3]);
								flag = true;
							}
							else
							{
								FamilyInstance fi = document.Create.NewCrossFitting(appropriateConnector, appropriateConnector2, appropriateConnector3, appropriateConnector4);
								this.ResizeFitting(fi);
								flag = true;
							}
						}
					}
				}
				ducts.Clear();
				ducts.Add(hypiping.Piping);
				ducts.Add(hypiping5.Piping);
				ducts.Add(hypiping3.Piping);
				ducts.Add(hypiping6.Piping);
				document.Regenerate();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				throw new Exception("创建管件连接失败！");
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		protected void ResizeFitting(FamilyInstance fi)
		{
			if (fi != null && fi.MEPModel != null)
			{
				int num = 0;
				int num2 = 0;
				foreach (object obj in fi.MEPModel.ConnectorManager.Connectors)
				{
					Connector connector = (Connector)obj;
					Connector connectedConnector = connector.GetConnectedConnector();
                    if ((int)connector.Shape == 1)
					{
						if (connectedConnector.Width == connector.Width && connectedConnector.Height == connector.Height)
						{
							num++;
						}
						if (connectedConnector.Width == connector.Height && connectedConnector.Height == connector.Width)
						{
							num2++;
						}
					}
				}
				foreach (object obj2 in fi.MEPModel.ConnectorManager.Connectors)
				{
					Connector connector2 = (Connector)obj2;
					Connector connectedConnector2 = connector2.GetConnectedConnector();
                    if ((int)connector2.Shape == 1)
					{
						if (num > num2)
						{
							if (connectedConnector2.Width != connector2.Width && connectedConnector2.Height != connector2.Height)
							{
								connector2.Width = connectedConnector2.Width;
								connector2.Height = connectedConnector2.Height;
							}
						}
						else if (num2 > num && connectedConnector2.Width != connector2.Height && connectedConnector2.Height != connector2.Width)
						{
							connector2.Width = connectedConnector2.Height;
							connector2.Height = connectedConnector2.Width;
						}
					}
				}
			}
		}

		public FamilyInstance NewCrossFitting(IList<Duct> ducts, IList<XYZ> xyzs, FamilySymbol fs)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance2 = null;
			Autodesk.Revit.DB.Document document = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count < 4)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[3], xyzs[3]);
				YJKPiping<Duct> hypiping5 = null;
				YJKPiping<Duct> hypiping6 = null;
				UnifiedModified.ActivateFamilySymbol(fs);
				familyInstance2 = document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				Connector hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance2);
				if (hypiping.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping2.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping3.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping4.StartConnector.Connector.Shape != hyelementConnector.Shape)
				{
					throw new Exception("管件和风管的截面类型不匹配");
				}
				hypiping.ChangeFirstCross(fs);
				hypiping2.ChangeFirstCross(fs);
				hypiping3.ChangeFirstCross(fs);
				hypiping4.ChangeFirstCross(fs);
				document.Regenerate();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, true);
				if (xyz != null)
				{
					if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
					{
						new List<Duct>();
						hypiping5 = new YJKPiping<Duct>(((IList<Duct>)hypiping.Separate(xyz))[1], xyzs[1]);
					}
					else if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsParallel(hypiping2.Piping))
					{
						hypiping5 = new YJKPiping<Duct>(hypiping2.Piping, xyzs[1]);
					}
					if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
					{
						new List<Duct>();
						hypiping6 = new YJKPiping<Duct>(((IList<Duct>)hypiping3.Separate(xyz))[1], xyzs[3]);
					}
					else if (hypiping3.IsPipingOnLine(hypiping4.Piping) || hypiping3.IsParallel(hypiping4.Piping))
					{
						hypiping6 = new YJKPiping<Duct>(hypiping4.Piping, xyzs[3]);
					}
					if (hypiping5 == null || hypiping6 == null)
					{
						throw new Exception("请检查风管位置是否正确");
					}
					Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
					Connector closeConnector2 = hypiping5.GetCloseConnector(xyz, false);
					Connector closeConnector3 = hypiping3.GetCloseConnector(xyz, false);
					Connector closeConnector4 = hypiping6.GetCloseConnector(xyz, false);
					if (closeConnector == null || closeConnector.IsConnected || closeConnector2 == null || closeConnector2.IsConnected || closeConnector3 == null || closeConnector3.IsConnected || closeConnector4 == null || closeConnector4.IsConnected)
					{
						throw new Exception("风管已经连接");
					}
					familyInstance = document.Create.NewCrossFitting(closeConnector, closeConnector2, closeConnector3, closeConnector4);
				}
				subTransaction.Commit();
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance2 != null)
				{
					document.DeleteElement(familyInstance2);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewCrossFitting(IList<Duct> ducts, IList<XYZ> xyzs, FamilySymbol fs, ref string connMSG)
		{
			SubTransaction subTransaction = null;
			FamilyInstance result;
			try
			{
				if (ducts.Count != 4)
				{
					connMSG = "请选择四根风管，才可以进行四通连接！";
					result = null;
				}
				else
				{
					Autodesk.Revit.DB.Document document = ducts[0].Document;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[3], xyzs[3]);
					YJKPiping<Duct> hypiping5 = null;
					YJKPiping<Duct> hypiping6 = null;
					UnifiedModified.ActivateFamilySymbol(fs);
					FamilyInstance familyInstance = document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
					document.Regenerate();
					Connector hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance);
					if (hypiping.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping2.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping3.StartConnector.Connector.Shape != hyelementConnector.Shape || hypiping4.StartConnector.Connector.Shape != hyelementConnector.Shape)
					{
						connMSG = "管件和风管的截面类型不匹配";
						if (familyInstance != null)
						{
							document.DeleteElement(familyInstance);
						}
						result = null;
					}
					else
					{
						if (familyInstance != null)
						{
							document.DeleteElement(familyInstance);
						}
						bool flag = hypiping.ChangeFirstCross(fs);
						bool flag2 = hypiping2.ChangeFirstCross(fs);
						bool flag3 = hypiping3.ChangeFirstCross(fs);
						bool flag4 = hypiping4.ChangeFirstCross(fs);
						if (!flag || !flag2 || !flag4 || !flag3)
						{
							connMSG = "设置四通连接件首先项失败！";
							result = null;
						}
						else
						{
							document.Regenerate();
							subTransaction = new SubTransaction(document);
							subTransaction.Start();
							XYZ xyz = hypiping.IntersectionPoint(hypiping3.Piping, true);
							if (xyz == null)
							{
								connMSG = "四根水管不能交与一点，无法连接！";
								result = null;
							}
							else
							{
								if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
								{
									new List<Duct>();
									hypiping5 = new YJKPiping<Duct>(((IList<Duct>)hypiping.Separate(xyz))[1], xyzs[1]);
								}
								else if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsParallel(hypiping2.Piping))
								{
									hypiping5 = new YJKPiping<Duct>(hypiping2.Piping, xyzs[1]);
								}
								if (hypiping5 == null)
								{
									connMSG = "第一根风管与第二根风管应在同一直线上，\n否则无法满足连接条件！";
									result = null;
								}
								else
								{
									if (hypiping3.Piping.UniqueId == hypiping4.Piping.UniqueId)
									{
										new List<Duct>();
										hypiping6 = new YJKPiping<Duct>(((IList<Duct>)hypiping3.Separate(xyz))[1], xyzs[3]);
									}
									else if (hypiping3.IsPipingOnLine(hypiping4.Piping) || hypiping3.IsParallel(hypiping4.Piping))
									{
										hypiping6 = new YJKPiping<Duct>(hypiping4.Piping, xyzs[3]);
									}
									if (hypiping6 == null)
									{
										connMSG = "第三根风管与第四根风管应在同一直线上，\n否则无法满足连接条件！";
										result = null;
									}
									else
									{
										Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
										Connector closeConnector2 = hypiping5.GetCloseConnector(xyz, false);
										Connector closeConnector3 = hypiping3.GetCloseConnector(xyz, false);
										Connector closeConnector4 = hypiping6.GetCloseConnector(xyz, false);
										if (closeConnector == null || closeConnector2 == null || closeConnector3 == null || closeConnector4 == null)
										{
											connMSG = "获取连接口失败！";
											result = null;
										}
										else if (closeConnector.IsConnected || closeConnector2.IsConnected || closeConnector3.IsConnected || closeConnector4.IsConnected)
										{
											connMSG = "风管已经连接！";
											result = null;
										}
										else
										{
											FamilyInstance familyInstance2 = document.Create.NewCrossFitting(closeConnector, closeConnector2, closeConnector3, closeConnector4);
											subTransaction.Commit();
											result = familyInstance2;
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		private List<Connector> OrderCrossConnector(List<Connector> crossCon, XYZ direction)
		{
			if (direction == null)
			{
				return crossCon;
			}
			List<Connector> list = new List<Connector>(4);
			foreach (Connector connector in crossCon)
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-direction))
				{
					list.Insert(0, connector);
				}
				else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(direction))
				{
					if (list.Count > 0)
					{
						if (list[0].CoordinateSystem.BasisZ.IsAlmostEqualTo(-direction))
						{
							list.Insert(1, connector);
						}
						else
						{
							list.Insert(0, connector);
						}
					}
					else
					{
						list.Insert(0, connector);
					}
				}
				else
				{
					list.Add(connector);
				}
			}
			return list;
		}

		private bool DuctsConnect(IList<YJKPiping<Duct>> orderDucts, Autodesk.Revit.DB.Document doc)
		{
			bool result = false;
			if (orderDucts.Count > 1)
			{
				try
				{
					IList<Duct> list = new List<Duct>();
					for (int i = 0; i < orderDucts.Count; i += 2)
					{
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(orderDucts[i].Piping);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(orderDucts[i + 1].Piping);
						list.Clear();
						IList<YJKPiping<Duct>> list2 = new List<YJKPiping<Duct>>();
						list2.Add(hypiping);
						list2.Add(hypiping2);
						YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
						switch (hypiping.CheckPipingsPosition(hypiping2.Piping))
						{
						case 10:
							if (hypiping.StartConnector.Connector.Shape == hypiping2.StartConnector.Connector.Shape)
							{
								bool flag;
								if (hypiping.Piping.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM) != null)
								{
									flag = (hypiping.StartConnector.Connector.Radius == hypiping2.StartConnector.Connector.Radius);
								}
								else
								{
									flag = (hypiping.StartConnector.Connector.Width == hypiping2.StartConnector.Connector.Width && hypiping.StartConnector.Connector.Height == hypiping2.StartConnector.Connector.Height);
								}
								if (flag)
								{
									hypiping.Combine(hypiping2.Piping);
								}
								else
								{
									list.Add(hypiping.Piping);
									list.Add(hypiping2.Piping);
									this.NewTransitionFitting(list);
									doc.Regenerate();
								}
							}
							else
							{
								list.Add(hypiping.Piping);
								list.Add(hypiping2.Piping);
								this.NewTransitionFitting(list);
								doc.Regenerate();
							}
							break;
						case 11:
						{
							IList<XYZ> appAndEndPoint = hypiping.GetAppAndEndPoint(hypiping2.Piping);
							if (appAndEndPoint != null && appAndEndPoint.Count > 0)
							{
								if (hypiping.IsPointOnPipingCurve(appAndEndPoint[1]) && !hypiping.StartConnector.Origin.IsAlmostEqualTo(appAndEndPoint[1]) && !hypiping.EndConnector.Origin.IsAlmostEqualTo(appAndEndPoint[1]))
								{
									Duct duct = YJKRevitTools.NewDuct(hypiping2.Piping, appAndEndPoint[0], appAndEndPoint[1]);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct);
									if (hypiping.BeRotateDuct(duct))
									{
										new YJKPiping<Duct>(duct).Rotate90();
									}
									list.Add(duct);
									list.Add(hypiping2.Piping);
									this.NewElbowFitting(list);
									list.Clear();
									list.Add(duct);
									list.Add(hypiping.Piping);
									list.Add(hypiping.Piping);
									this.NewTeeFitting(list);
								}
								else if (hypiping2.IsPointOnPipingCurve(appAndEndPoint[1]) && !hypiping2.StartConnector.Origin.IsAlmostEqualTo(appAndEndPoint[1]) && !hypiping2.EndConnector.Origin.IsAlmostEqualTo(appAndEndPoint[1]))
								{
									Duct duct = YJKRevitTools.NewDuct(hypiping.Piping, appAndEndPoint[0], appAndEndPoint[1]);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct);
									if (hypiping.BeRotateDuct(duct))
									{
										new YJKPiping<Duct>(duct).Rotate90();
									}
									list.Add(duct);
									list.Add(hypiping.Piping);
									this.NewElbowFitting(list);
									list.Clear();
									list.Add(duct);
									list.Add(hypiping2.Piping);
									list.Add(hypiping2.Piping);
									this.NewTeeFitting(list);
								}
								else
								{
									Duct duct = YJKRevitTools.NewDuct(hypiping.Piping, appAndEndPoint[0], appAndEndPoint[1]);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct);
									if (hypiping.BeRotateDuct(duct))
									{
										new YJKPiping<Duct>(duct).Rotate90();
									}
									list.Add(duct);
									list.Add(hypiping.Piping);
									this.NewElbowFitting(list);
									list.Clear();
									list.Add(duct);
									list.Add(hypiping2.Piping);
									this.NewElbowFitting(list);
								}
							}
							else
							{
								IList<Connector> appropriateConnector = hypiping.GetAppropriateConnector(hypiping2.Piping);
								if (appropriateConnector != null && appropriateConnector.Count > 1)
								{
									XYZ point = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
									XYZ lineProject = hypiping.GetLineProject(point);
									XYZ lineProject2 = hypiping2.GetLineProject(point);
									Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, lineProject, lineProject2);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct2);
									YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct2);
									if (hypiping.BeRotateDuct(hypiping3.Piping))
									{
										hypiping3.Rotate90();
									}
									list.Add(hypiping.Piping);
									list.Add(hypiping3.Piping);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(hypiping2.Piping);
									list.Add(hypiping3.Piping);
									this.NewElbowFitting(list);
									doc.Regenerate();
								}
							}
							break;
						}
						case 12:
							list.Add(hypiping.Piping);
							list.Add(hypiping2.Piping);
							this.NewElbowFitting(list);
							break;
						case 13:
						{
							list.Add(hypiping.Piping);
							list.Add(hypiping2.Piping);
							Duct pointOnDuct = this.GetPointOnDuct(list);
							if (pointOnDuct != null)
							{
								list.Add(pointOnDuct);
							}
							this.NewTeeFitting(list);
							break;
						}
						case 14:
							list.Add(hypiping.Piping);
							list.Add(hypiping.Piping);
							list.Add(hypiping2.Piping);
							list.Add(hypiping2.Piping);
							this.NewCrossFitting(list, null);
							break;
						case 20:
						{
							IList<XYZ> endAndInsPoint = this.GetEndAndInsPoint(list2);
							if (endAndInsPoint != null && endAndInsPoint.Count > 0)
							{
								if (hypiping.IsPointOnPipingCurve(endAndInsPoint[1]))
								{
									Duct duct3 = YJKRevitTools.NewDuct(hypiping2.Piping, endAndInsPoint[0], endAndInsPoint[1]);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct3);
									YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct3);
									if (hypiping.BeRotateDuct(hypiping4.Piping))
									{
										hypiping4.Rotate90();
									}
									list.Add(hypiping4.Piping);
									list.Add(hypiping2.Piping);
									this.NewElbowFitting(list);
									list.Clear();
									list.Add(hypiping4.Piping);
									list.Add(hypiping.Piping);
									list.Add(hypiping.Piping);
									this.NewTeeFitting(list);
								}
								else
								{
									Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, endAndInsPoint[0], endAndInsPoint[1]);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct3);
									YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct3);
									if (hypiping.BeRotateDuct(hypiping5.Piping))
									{
										hypiping5.Rotate90();
									}
									list.Add(hypiping5.Piping);
									list.Add(hypiping.Piping);
									this.NewElbowFitting(list);
									list.Clear();
									list.Add(hypiping5.Piping);
									list.Add(hypiping2.Piping);
									list.Add(hypiping2.Piping);
									this.NewTeeFitting(list);
								}
							}
							else
							{
								IList<Connector> appropriateConnector2 = hypiping.GetAppropriateConnector(hypiping2.Piping);
								if (appropriateConnector2 != null && appropriateConnector2.Count > 1)
								{
									XYZ point2 = (appropriateConnector2[0].Origin + appropriateConnector2[1].Origin) / 2.0;
									XYZ lineProject3 = hypiping.GetLineProject(point2);
									XYZ lineProject4 = hypiping2.GetLineProject(point2);
									Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, lineProject3, lineProject4);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct4);
									YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(duct4);
									if (hypiping.BeRotateDuct(hypiping6.Piping))
									{
										hypiping6.Rotate90();
									}
									list.Add(hypiping.Piping);
									list.Add(hypiping6.Piping);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(hypiping2.Piping);
									list.Add(hypiping6.Piping);
									this.NewElbowFitting(list);
									doc.Regenerate();
								}
							}
							break;
						}
						case 21:
						{
							IList<XYZ> appAndEndPoint2 = hypiping.GetAppAndEndPoint(hypiping2.Piping);
							if (appAndEndPoint2 != null && appAndEndPoint2.Count > 0)
							{
								XYZ endXYZ = new XYZ(appAndEndPoint2[1].X, appAndEndPoint2[1].Y, appAndEndPoint2[0].Z);
								if (hypiping.IsPointOnPipingCurve(appAndEndPoint2[1]))
								{
									Duct duct5 = YJKRevitTools.NewDuct(hypiping2.Piping, appAndEndPoint2[0], endXYZ);
									Duct duct6 = YJKRevitTools.NewDuct(hypiping2.Piping, appAndEndPoint2[1], endXYZ);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct5);
									hyrotateFamilyInstance.MovePiping(duct6);
									if (hypiping.BeRotateDuct(duct5))
									{
										new YJKPiping<Duct>(duct5).Rotate90();
									}
									if (hypiping.BeRotateDuct(duct6))
									{
										new YJKPiping<Duct>(duct6).Rotate90();
									}
									list.Add(hypiping2.Piping);
									list.Add(duct5);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(duct5);
									list.Add(duct6);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(hypiping.Piping);
									list.Add(hypiping.Piping);
									list.Add(duct6);
									this.NewTeeFitting(list);
								}
								else
								{
									Duct duct7 = YJKRevitTools.NewDuct(hypiping.Piping, appAndEndPoint2[0], endXYZ);
									Duct duct8 = YJKRevitTools.NewDuct(hypiping.Piping, appAndEndPoint2[1], endXYZ);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct7);
									hyrotateFamilyInstance.MovePiping(duct8);
									if (hypiping.BeRotateDuct(duct7))
									{
										new YJKPiping<Duct>(duct7).Rotate90();
									}
									if (hypiping.BeRotateDuct(duct8))
									{
										new YJKPiping<Duct>(duct8).Rotate90();
									}
									list.Add(hypiping.Piping);
									list.Add(duct7);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(duct7);
									list.Add(duct8);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(hypiping2.Piping);
									list.Add(hypiping2.Piping);
									list.Add(duct8);
									this.NewTeeFitting(list);
								}
							}
							else
							{
								IList<XYZ> point3 = this.GetPoint21(list2);
								if (point3 != null && point3.Count > 0)
								{
									Duct duct9 = YJKRevitTools.NewDuct(hypiping.Piping, point3[0], point3[2]);
									Duct duct10 = YJKRevitTools.NewDuct(hypiping.Piping, point3[1], point3[2]);
									doc.Regenerate();
									hyrotateFamilyInstance.MovePiping(duct9);
									hyrotateFamilyInstance.MovePiping(duct10);
									if (hypiping.BeRotateDuct(duct9))
									{
										new YJKPiping<Duct>(duct9).Rotate90();
									}
									if (hypiping.BeRotateDuct(duct10))
									{
										new YJKPiping<Duct>(duct10).Rotate90();
									}
									if (hypiping.IsPointOnPipingLine(point3[0]))
									{
										list.Add(hypiping.Piping);
										list.Add(duct9);
										this.NewElbowFitting(list);
										doc.Regenerate();
										list.Clear();
										list.Add(duct9);
										list.Add(duct10);
										this.NewElbowFitting(list);
										doc.Regenerate();
										list.Clear();
										list.Add(duct10);
										list.Add(hypiping2.Piping);
										this.NewElbowFitting(list);
										doc.Regenerate();
									}
									else
									{
										list.Add(hypiping.Piping);
										list.Add(duct10);
										this.NewElbowFitting(list);
										doc.Regenerate();
										list.Clear();
										list.Add(duct9);
										list.Add(duct10);
										this.NewElbowFitting(list);
										doc.Regenerate();
										list.Clear();
										list.Add(duct9);
										list.Add(hypiping2.Piping);
										this.NewElbowFitting(list);
										doc.Regenerate();
									}
								}
							}
							break;
						}
						case 22:
						{
							List<YJKPiping<Duct>> newDuct = this.GetNewDuct(list2, doc);
							list.Add(hypiping.Piping);
							list.Add(newDuct[0].Piping);
							this.NewElbowFitting(list);
							list.Clear();
							list.Add(hypiping2.Piping);
							list.Add(newDuct[1].Piping);
							this.NewElbowFitting(list);
							break;
						}
						case 23:
						{
							List<YJKPiping<Duct>> newDuct = this.GetNewDuct(list2, doc);
							if (hypiping.IsPointOnPipingCurve(newDuct[0].StartConnector.Origin) || hypiping.IsPointOnPipingCurve(newDuct[0].EndConnector.Origin))
							{
								list.Add(hypiping.Piping);
								list.Add(hypiping.Piping);
								list.Add(newDuct[0].Piping);
								this.NewTeeFitting(list);
								list.Clear();
								list.Add(hypiping2.Piping);
								list.Add(newDuct[1].Piping);
								this.NewElbowFitting(list);
							}
							else
							{
								list.Add(hypiping.Piping);
								list.Add(newDuct[0].Piping);
								this.NewElbowFitting(list);
								list.Clear();
								list.Add(hypiping2.Piping);
								list.Add(hypiping2.Piping);
								list.Add(newDuct[1].Piping);
								this.NewTeeFitting(list);
							}
							break;
						}
						case 24:
						{
							List<YJKPiping<Duct>> newDuct = this.GetNewDuct(list2, doc);
							list.Add(hypiping.Piping);
							list.Add(hypiping.Piping);
							list.Add(newDuct[0].Piping);
							this.NewTeeFitting(list);
							list.Clear();
							list.Add(hypiping2.Piping);
							list.Add(hypiping2.Piping);
							list.Add(newDuct[1].Piping);
							this.NewTeeFitting(list);
							break;
						}
						}
						result = true;
					}
					return result;
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
			throw new Exception("没有找到适合的管道匹配类型!");
		}

		public bool NewBeatchFitting(IList<Duct> ducts)
		{
			bool result;
			try
			{
				if (ducts == null || ducts.Count < 2)
				{
					throw new System.ArgumentNullException("Duct", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				IList<YJKPiping<Duct>> orderDucts = new List<YJKPiping<Duct>>();
				GroupPipe<Duct> groupPipe = new GroupPipe<Duct>(ducts);
				groupPipe.Execute();
				orderDucts = groupPipe.sortedPipes;
				result = this.DuctsConnect(orderDucts, document);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool NewSomeConnect(IList<Duct> ducts)
		{
			bool flag = true;
			bool result;
			try
			{
				if (ducts == null || ducts.Count < 2)
				{
					throw new Exception("最少选择2根风管");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				IList<YJKPiping<Duct>> list = new List<YJKPiping<Duct>>();
				list = this.SomeDuctsOrderBy(ducts);
				if (list != null && list.Count > 1)
				{
					flag = this.DuctsConnect(list, document);
				}
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private IList<XYZ> GetPoint21(IList<YJKPiping<Duct>> hyDucts)
		{
			IList<XYZ> list = new List<XYZ>();
			IList<Connector> appropriateConnector = hyDucts[0].GetAppropriateConnector(hyDucts[1].Piping);
			XYZ xyz = null;
			if (appropriateConnector != null && appropriateConnector.Count > 1)
			{
				if (!appropriateConnector[0].IsConnected && !appropriateConnector[1].IsConnected)
				{
					xyz = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
				}
				else if (appropriateConnector[0].IsConnected)
				{
					xyz = hyDucts[0].GetDistanceConnector(appropriateConnector[0].Origin).Origin;
				}
				else
				{
					xyz = hyDucts[1].GetDistanceConnector(appropriateConnector[1].Origin).Origin;
				}
			}
			if (xyz != null)
			{
				XYZ project = hyDucts[0].GetProject(xyz, true);
				XYZ project2 = hyDucts[1].GetProject(xyz, true);
				XYZ item = new XYZ(project2.X, project2.Y, project.Z);
				list.Add(project);
				list.Add(project2);
				list.Add(item);
			}
			return list;
		}

		private bool IsSameShapeAndType(YJKPiping<Duct> hyDuct1, YJKPiping<Duct> hyDuct2)
		{
			bool result;
			if (hyDuct2.Piping.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM) != null)
			{
				result = (hyDuct2.StartConnector.Connector.Radius == hyDuct1.StartConnector.Connector.Radius);
			}
			else
			{
				result = (hyDuct2.StartConnector.Connector.Width == hyDuct1.StartConnector.Connector.Width && hyDuct2.StartConnector.Connector.Height == hyDuct1.StartConnector.Connector.Height);
			}
			return result;
		}

		private IList<YJKPiping<Duct>> SomeDuctsOrderBy(IList<Duct> ducts)
		{
			IList<YJKPiping<Duct>> list = new List<YJKPiping<Duct>>();
			switch (ducts.Count)
			{
			case 1:
				throw new Exception("请选择多根风管");
			case 2:
			{
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
				if (hypiping.IsPipingOnLine(hypiping2.Piping))
				{
					IList<Duct> list2 = new List<Duct>();
					if (hypiping.StartConnector.Connector.Shape == hypiping2.StartConnector.Connector.Shape)
					{
						if (this.IsSameShapeAndType(hypiping, hypiping2))
						{
							hypiping.Combine(hypiping2.Piping);
						}
						else
						{
							list2.Add(hypiping.Piping);
							list2.Add(hypiping2.Piping);
							this.NewTransitionFitting(list2);
						}
					}
					else
					{
						list2.Add(hypiping.Piping);
						list2.Add(hypiping2.Piping);
						this.NewTransitionFitting(list2);
					}
				}
				else
				{
					list.Add(hypiping);
					list.Add(hypiping2);
				}
				break;
			}
			case 3:
			{
				YJKPiping<Duct> hyDuct = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hyDuct2 = new YJKPiping<Duct>(ducts[1]);
				YJKPiping<Duct> hyDuct3 = new YJKPiping<Duct>(ducts[2]);
				list = this.OrderThreeDuct(hyDuct, hyDuct2, hyDuct3);
				break;
			}
			case 4:
			{
				IList<Duct> list3 = new List<Duct>();
				int num = 0;
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[1]);
				YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(ducts[2]);
				YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(ducts[3]);
				if (hypiping3.StartConnector.Connector.Shape != hypiping4.StartConnector.Connector.Shape || hypiping3.StartConnector.Connector.Shape != hypiping5.StartConnector.Connector.Shape || hypiping3.StartConnector.Connector.Shape != hypiping6.StartConnector.Connector.Shape)
				{
					throw new Exception("风管的截面类型不匹配");
				}
				if (hypiping3.IsPipingOnLine(hypiping4.Piping))
				{
					if (this.IsSameShapeAndType(hypiping3, hypiping4) && hypiping3.GetSystemElementId() == hypiping4.GetSystemElementId())
					{
						hypiping3.Combine(hypiping4.Piping);
						num++;
					}
					if (this.IsSameShapeAndType(hypiping5, hypiping6) && hypiping5.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping5.Combine(hypiping6.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ducts, null);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping5.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping6.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping5.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping5.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping5);
					}
				}
				else if (hypiping3.IsPipingOnLine(hypiping5.Piping))
				{
					if (this.IsSameShapeAndType(hypiping3, hypiping5) && hypiping3.GetSystemElementId() == hypiping5.GetSystemElementId())
					{
						hypiping3.Combine(hypiping5.Piping);
						num++;
					}
					if (this.IsSameShapeAndType(hypiping4, hypiping6) && hypiping4.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping4.Combine(hypiping6.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ducts, null);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping6.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping4.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping4);
					}
				}
				else if (hypiping3.IsPipingOnLine(hypiping6.Piping))
				{
					if (this.IsSameShapeAndType(hypiping3, hypiping6) && hypiping3.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping3.Combine(hypiping6.Piping);
						num++;
					}
					if (this.IsSameShapeAndType(hypiping4, hypiping5) && hypiping4.GetSystemElementId() == hypiping5.GetSystemElementId())
					{
						hypiping4.Combine(hypiping5.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ducts, null);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping5.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping4.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping4);
					}
				}
				else if (hypiping4.IsPipingOnLine(hypiping5.Piping))
				{
					if (this.IsSameShapeAndType(hypiping4, hypiping5) && hypiping4.GetSystemElementId() == hypiping5.GetSystemElementId())
					{
						hypiping4.Combine(hypiping5.Piping);
						num++;
					}
					if (this.IsSameShapeAndType(hypiping3, hypiping6) && hypiping3.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping3.Combine(hypiping6.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ducts, null);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping6.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping4);
					}
				}
				else if (hypiping4.IsPipingOnLine(hypiping6.Piping))
				{
					if (hypiping4.Piping.Diameter == hypiping6.Piping.Diameter && hypiping4.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping4.Combine(hypiping6.Piping);
						num++;
					}
					if (hypiping3.Piping.Diameter == hypiping5.Piping.Diameter && hypiping3.GetSystemElementId() == hypiping5.GetSystemElementId())
					{
						hypiping3.Combine(hypiping5.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ducts, null);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping5.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping4.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping4.Piping);
						list3.Add(hypiping6.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping4);
					}
				}
				else if (hypiping5.IsPipingOnLine(hypiping6.Piping))
				{
					if (hypiping5.Piping.Diameter == hypiping6.Piping.Diameter && hypiping5.GetSystemElementId() == hypiping6.GetSystemElementId())
					{
						hypiping5.Combine(hypiping6.Piping);
						num++;
					}
					if (hypiping3.Piping.Diameter == hypiping4.Piping.Diameter && hypiping3.GetSystemElementId() == hypiping4.GetSystemElementId())
					{
						hypiping3.Combine(hypiping4.Piping);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(ducts, null);
					}
					else if (1 == num)
					{
						if (!hypiping3.IsIntersection(hypiping5.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping4.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (2 == num)
					{
						if (!hypiping3.IsIntersection(hypiping5.Piping))
						{
							throw new Exception("请检查管道是否具有不同的管径或系统类型，并且不在同一平面上");
						}
						list3.Add(hypiping5.Piping);
						list3.Add(hypiping6.Piping);
						list3.Add(hypiping3.Piping);
						list3.Add(hypiping3.Piping);
						this.NewCrossFitting(list3, null);
					}
					else if (3 == num)
					{
						list.Add(hypiping3);
						list.Add(hypiping5);
					}
				}
				else if (hypiping3.IsIntersection(hypiping4.Piping))
				{
					list.Add(hypiping3);
					list.Add(hypiping4);
				}
				else if (hypiping3.IsIntersection(hypiping5.Piping))
				{
					list.Add(hypiping3);
					list.Add(hypiping5);
				}
				else if (hypiping3.IsIntersection(hypiping6.Piping))
				{
					list.Add(hypiping3);
					list.Add(hypiping6);
				}
				else if (hypiping4.IsIntersection(hypiping5.Piping))
				{
					list.Add(hypiping4);
					list.Add(hypiping5);
				}
				else if (hypiping4.IsIntersection(hypiping6.Piping))
				{
					list.Add(hypiping4);
					list.Add(hypiping6);
				}
				else if (hypiping5.IsIntersection(hypiping6.Piping))
				{
					list.Add(hypiping5);
					list.Add(hypiping6);
				}
				else
				{
					list.Add(hypiping3);
					list.Add(hypiping4);
				}
				break;
			}
			default:
				throw new Exception("风管不能超过四根");
			}
			return list;
		}

		private IList<YJKPiping<Duct>> OrderThreeDuct(YJKPiping<Duct> hyDuct, YJKPiping<Duct> hyDuct1, YJKPiping<Duct> hyDuct2)
		{
			IList<YJKPiping<Duct>> list = new List<YJKPiping<Duct>>();
			IList<Duct> list2 = new List<Duct>();
			list2.Add(hyDuct.Piping);
			list2.Add(hyDuct1.Piping);
			list2.Add(hyDuct2.Piping);
			if (hyDuct.StartConnector.Connector.Shape != hyDuct1.StartConnector.Connector.Shape || hyDuct1.StartConnector.Connector.Shape != hyDuct2.StartConnector.Connector.Shape)
			{
				throw new Exception("风管的截面类型不匹配");
			}
			if (hyDuct.IsPipingOnLine(hyDuct1.Piping))
			{
				if (this.IsSameShapeAndType(hyDuct, hyDuct1) && hyDuct.GetSystemElementId() == hyDuct1.GetSystemElementId())
				{
					hyDuct.Combine(hyDuct1.Piping);
					list.Add(hyDuct);
					list.Add(hyDuct2);
				}
				else
				{
					this.NewTeeFitting(list2);
					list = null;
				}
			}
			else if (hyDuct.IsPipingOnLine(hyDuct2.Piping))
			{
				if (this.IsSameShapeAndType(hyDuct, hyDuct2) && hyDuct.GetSystemElementId() == hyDuct2.GetSystemElementId())
				{
					hyDuct.Combine(hyDuct2.Piping);
					list.Add(hyDuct);
					list.Add(hyDuct1);
				}
				else
				{
					this.NewTeeFitting(list2);
					list = null;
				}
			}
			else if (hyDuct1.IsPipingOnLine(hyDuct2.Piping))
			{
				if (this.IsSameShapeAndType(hyDuct1, hyDuct2) && hyDuct1.GetSystemElementId() == hyDuct2.GetSystemElementId())
				{
					hyDuct1.Combine(hyDuct2.Piping);
					list.Add(hyDuct);
					list.Add(hyDuct1);
				}
				else
				{
					this.NewTeeFitting(list2);
					list = null;
				}
			}
			else if (hyDuct.IsIntersection(hyDuct1.Piping))
			{
				list.Add(hyDuct);
				list.Add(hyDuct1);
			}
			else if (hyDuct.IsIntersection(hyDuct2.Piping))
			{
				list.Add(hyDuct);
				list.Add(hyDuct2);
			}
			else if (hyDuct1.IsIntersection(hyDuct2.Piping))
			{
				list.Add(hyDuct2);
				list.Add(hyDuct1);
			}
			else
			{
				list.Add(hyDuct);
				list.Add(hyDuct1);
			}
			return list;
		}

		private IList<XYZ> GetEndAndInsPoint(IList<YJKPiping<Duct>> hyDucts)
		{
			IList<XYZ> list = new List<XYZ>();
			YJKPiping<Duct> hypiping = hyDucts[0];
			YJKPiping<Duct> hypiping2 = hyDucts[1];
			bool flag = false;
			if (hypiping.GetDiameter() > hypiping2.GetDiameter())
			{
				if (!hypiping2.StartConnector.Connector.IsConnected)
				{
					XYZ xyz = new XYZ(hypiping2.StartConnector.Origin.X, hypiping2.StartConnector.Origin.Y, hypiping.StartConnector.Origin.Z);
					XYZ project = hypiping.GetProject(hypiping2.StartConnector.Origin);
					if (project != null && xyz.IsAlmostEqualTo(project))
					{
						list.Add(hypiping2.StartConnector.Origin);
						list.Add(project);
						flag = true;
					}
				}
				if (!hypiping2.EndConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz2 = new XYZ(hypiping2.EndConnector.Origin.X, hypiping2.EndConnector.Origin.Y, hypiping.EndConnector.Origin.Z);
					XYZ project2 = hypiping.GetProject(hypiping2.EndConnector.Origin);
					if (project2 != null && xyz2.IsAlmostEqualTo(project2))
					{
						list.Add(hypiping2.EndConnector.Origin);
						list.Add(project2);
						flag = true;
					}
				}
				if (!hypiping.StartConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz3 = new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, hypiping2.StartConnector.Origin.Z);
					XYZ project3 = hypiping2.GetProject(hypiping.StartConnector.Origin);
					if (project3 != null && xyz3.IsAlmostEqualTo(project3))
					{
						list.Add(hypiping.StartConnector.Origin);
						list.Add(project3);
						flag = true;
					}
				}
				if (!hypiping.EndConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz4 = new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, hypiping2.EndConnector.Origin.Z);
					XYZ project4 = hypiping2.GetProject(hypiping.EndConnector.Origin);
					if (project4 != null && xyz4.IsAlmostEqualTo(project4))
					{
						list.Add(hypiping.EndConnector.Origin);
						list.Add(project4);
					}
				}
			}
			else
			{
				if (!hypiping.StartConnector.Connector.IsConnected)
				{
					XYZ xyz5 = new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, hypiping2.StartConnector.Origin.Z);
					XYZ project5 = hypiping2.GetProject(hypiping.StartConnector.Origin);
					if (project5 != null && xyz5.IsAlmostEqualTo(project5))
					{
						list.Add(hypiping.StartConnector.Origin);
						list.Add(project5);
						flag = true;
					}
				}
				if (!hypiping.EndConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz6 = new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, hypiping2.EndConnector.Origin.Z);
					XYZ project6 = hypiping2.GetProject(hypiping.EndConnector.Origin);
					if (project6 != null && xyz6.IsAlmostEqualTo(project6))
					{
						list.Add(hypiping.EndConnector.Origin);
						list.Add(project6);
						flag = true;
					}
				}
				if (!hypiping2.StartConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz7 = new XYZ(hypiping2.StartConnector.Origin.X, hypiping2.StartConnector.Origin.Y, hypiping.StartConnector.Origin.Z);
					XYZ project7 = hypiping.GetProject(hypiping2.StartConnector.Origin);
					if (project7 != null && xyz7.IsAlmostEqualTo(project7))
					{
						list.Add(hypiping2.StartConnector.Origin);
						list.Add(project7);
						flag = true;
					}
				}
				if (!hypiping2.EndConnector.Connector.IsConnected && !flag)
				{
					XYZ xyz8 = new XYZ(hypiping2.EndConnector.Origin.X, hypiping2.EndConnector.Origin.Y, hypiping.EndConnector.Origin.Z);
					XYZ project8 = hypiping.GetProject(hypiping2.EndConnector.Origin);
					if (project8 != null && xyz8.IsAlmostEqualTo(project8))
					{
						list.Add(hypiping2.EndConnector.Origin);
						list.Add(project8);
					}
				}
			}
			return list;
		}

		private List<YJKPiping<Duct>> GetNewDuct(IList<YJKPiping<Duct>> hyDucts, Autodesk.Revit.DB.Document doc)
		{
			List<YJKPiping<Duct>> list = new List<YJKPiping<Duct>>();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			XYZ defferentPlaneInstersection = hyDucts[0].GetDefferentPlaneInstersection(hyDucts[1].Piping);
			XYZ defferentPlaneInstersection2 = hyDucts[1].GetDefferentPlaneInstersection(hyDucts[0].Piping);
			Duct duct = (hyDucts[0].GetDiameter() < hyDucts[1].GetDiameter()) ? hyDucts[0].Piping : hyDucts[1].Piping;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			Connector closeConnector = hyDucts[0].GetCloseConnector(defferentPlaneInstersection, false);
			Connector closeConnector2 = hyDucts[1].GetCloseConnector(defferentPlaneInstersection2, false);
			if (closeConnector.IsConnected && !hyDucts[0].IsPointOnPipingCurve(defferentPlaneInstersection))
			{
				Connector distanceConnector = hyDucts[0].GetDistanceConnector(defferentPlaneInstersection);
				XYZ xyz = (distanceConnector.Origin - closeConnector.Origin).Normalize();
				double num = distanceConnector.Origin.DistanceTo(defferentPlaneInstersection) + 0.3;
				XYZ startXYZ = defferentPlaneInstersection + xyz * num;
				XYZ endXYZ = defferentPlaneInstersection2 + xyz * num;
				Duct duct2 = YJKRevitTools.NewDuct(duct, startXYZ, endXYZ);
				Duct duct3 = YJKRevitTools.NewDuct(duct, defferentPlaneInstersection2, endXYZ);
				doc.Regenerate();
				hyrotateFamilyInstance.MovePiping(duct2);
				hyrotateFamilyInstance.MovePiping(duct3);
				if (hypiping.BeRotateDuct(duct2))
				{
					new YJKPiping<Duct>(duct2).Rotate90();
				}
				if (hypiping.BeRotateDuct(duct3))
				{
					new YJKPiping<Duct>(duct3).Rotate90();
				}
				this.NewElbowFitting(new List<Duct>
				{
					duct2,
					duct3
				});
				list.Add(new YJKPiping<Duct>(duct2));
				list.Add(new YJKPiping<Duct>(duct3));
			}
			else if (closeConnector2.IsConnected && !hyDucts[1].IsPointOnPipingCurve(defferentPlaneInstersection2))
			{
				Connector distanceConnector2 = hyDucts[1].GetDistanceConnector(defferentPlaneInstersection2);
				XYZ xyz2 = (distanceConnector2.Origin - closeConnector2.Origin).Normalize();
				double num2 = distanceConnector2.Origin.DistanceTo(defferentPlaneInstersection2) + 0.3;
				XYZ xyz3 = defferentPlaneInstersection + xyz2 * num2;
				XYZ endXYZ2 = defferentPlaneInstersection2 + xyz2 * num2;
				Duct duct4 = YJKRevitTools.NewDuct(duct, xyz3, endXYZ2);
				Duct duct5 = YJKRevitTools.NewDuct(duct, defferentPlaneInstersection, xyz3);
				doc.Regenerate();
				hyrotateFamilyInstance.MovePiping(duct4);
				hyrotateFamilyInstance.MovePiping(duct5);
				if (hypiping.BeRotateDuct(duct4))
				{
					new YJKPiping<Duct>(duct4).Rotate90();
				}
				if (hypiping.BeRotateDuct(duct5))
				{
					new YJKPiping<Duct>(duct5).Rotate90();
				}
				this.NewElbowFitting(new List<Duct>
				{
					duct4,
					duct5
				});
				list.Add(new YJKPiping<Duct>(duct5));
				list.Add(new YJKPiping<Duct>(duct4));
			}
			else
			{
				Duct duct6 = YJKRevitTools.NewDuct(duct, defferentPlaneInstersection, defferentPlaneInstersection2);
				hyrotateFamilyInstance.MovePiping(duct6);
				doc.Regenerate();
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct6);
				if (hypiping.BeRotateDuct(hypiping2.Piping))
				{
					hypiping2.Rotate90();
				}
				list.Add(hypiping2);
				list.Add(hypiping2);
			}
			return list;
		}

		private Duct GetPointOnDuct(IList<Duct> hyDucts)
		{
			Duct result = null;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(hyDucts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(hyDucts[1]);
			if (hyDucts.Count == 2)
			{
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping);
				if (xyz != null)
				{
					if (hypiping.IsPointOnPipingCurve(xyz))
					{
						result = hypiping.Piping;
					}
					else if (hypiping2.IsPointOnPipingCurve(xyz))
					{
						result = hypiping2.Piping;
					}
				}
			}
			return result;
		}

		private XYZ GetLocationPoint(FamilyInstance fi)
		{
			return (fi.Location as LocationPoint).Point;
		}

		private double GetConnectWidth(Connector connector)
		{
			double result = 0.0;
			if (connector.Shape == ConnectorSharp.Rect)
			{
				result = connector.Width;
			}
			else if (connector.Shape == ConnectorSharp.Round)
			{
				result = 2.0 * connector.Radius;
			}
			return result;
		}

		private XYZ GetConnectorDirectionPoint(Connector connector, int x = 2)
		{
			XYZ result = null;
			if (connector != null)
			{
				Transform coordinateSystem = connector.CoordinateSystem;
				result = connector.Origin + (double)x * this.GetConnectWidth(connector) * coordinateSystem.BasisZ;
			}
			return result;
		}

		private XYZ GetConnectorDirectionPoint(Connector connector, double distance)
		{
			XYZ result = null;
			if (connector != null)
			{
				Transform coordinateSystem = connector.CoordinateSystem;
				result = connector.Origin + distance * coordinateSystem.BasisZ;
			}
			return result;
		}

		private IList<YJKPiping<Duct>> DuctsOrderByType(IList<Duct> dcuts)
		{
			IList<YJKPiping<Duct>> list = new List<YJKPiping<Duct>>();
			IList<YJKPiping<Duct>> list2 = new List<YJKPiping<Duct>>();
			foreach (Duct piping in dcuts)
			{
				list.Add(new YJKPiping<Duct>(piping));
			}
			list = (from c in list
			orderby c.GetSystemElementId().IntegerValue
			select c).ToList<YJKPiping<Duct>>();
			int count = list.Count;
			for (int i = 0; i < count; i += 2)
			{
				if (i + 1 < count)
				{
					if (list[i].GetSystemElementId().IntegerValue == list[i + 1].GetSystemElementId().IntegerValue)
					{
						list2.Add(list[i]);
						list2.Add(list[i + 1]);
						for (int j = i + 1; j < count; j++)
						{
							if (j + 1 < count && list[j].GetSystemElementId().IntegerValue != list[j + 1].GetSystemElementId().IntegerValue)
							{
								i = j - 1;
								break;
							}
						}
					}
					else
					{
						i--;
					}
				}
			}
			return list2;
		}

		private XYZ GetCurveIntersection(Curve curve1, Curve curve2)
		{
			XYZ result = new XYZ();
			result = null;
			IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
			SetComparisonResult setComparisonResult = curve1.Intersect(curve2, out intersectionResultArray);
			if (4 != (int)setComparisonResult && intersectionResultArray != null && !intersectionResultArray.IsEmpty)
			{
				result = intersectionResultArray.get_Item(0).XYZPoint;
			}
			return result;
		}

		private XYZ GetCurveIntersection(Curve curve1, Curve curve2, double deviation)
		{
			XYZ result = new XYZ();
			result = null;
			XYZ dlineP = YJKRevitTools.GetDLineP(curve1.GetEndPoint(0), curve1.GetEndPoint(1), curve2.GetEndPoint(0), curve2.GetEndPoint(1));
			curve1.MakeUnbound();
			if (curve1.Distance(dlineP) <= deviation)
			{
				result = dlineP;
			}
			return result;
		}

		public bool DuctConnectAirTerminalElbow(Duct duct, FamilyInstance airTerminal, int terminalConnType)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2, 0.001);
				if (curveIntersection != null && !validConnector.IsConnected)
				{
					Duct duct2;
					if (terminalConnType == 0)
					{
						duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
						this.DuctSetParameter(validConnector, duct2, false);
						document.Regenerate();
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
						if (!hypiping.IsYAxis())
						{
							double xaxisAngle = hypiping.GetXAxisAngle();
							hypiping2.Rotate(Math.PI - xaxisAngle);
						}
						Connector closeConnector = hypiping2.GetCloseConnector(validConnector.Origin, false);
						if (!this.IsConnectSameDirection(validConnector, closeConnector))
						{
							hypiping2.Rotate(Math.PI*.5);
						}
						closeConnector.ConnectTo(validConnector);
						document.Regenerate();
					}
					else
					{
						duct2 = this.FlexDuctConnection(document, curveIntersection, validConnector, hypiping);
					}
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct2);
					Connector closeConnector2 = hypiping.GetCloseConnector(curveIntersection, false);
					Connector closeConnector3 = hypiping3.GetCloseConnector(curveIntersection, false);
					if (closeConnector2 != null && closeConnector3 != null && !closeConnector2.IsConnected && !closeConnector3.IsConnected)
					{
						document.Create.NewElbowFitting(closeConnector2, closeConnector3);
						flag = true;
					}
				}
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctConnectAirTerminalElbow(Duct duct, FamilyInstance airTerminal, int terminalConnType, ref string connMSG)
		{
			bool flag = false;
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2, 0.001);
				if (curveIntersection != null && !validConnector.IsConnected)
				{
					Duct duct2;
					if (terminalConnType == 0)
					{
						duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
					}
					else
					{
						duct2 = this.FlexDuctConnection(document, curveIntersection, validConnector, hypiping);
					}
					this.DuctSetParameter(validConnector, duct2, false);
					document.Regenerate();
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					Connector closeConnector = hypiping.GetCloseConnector(curveIntersection, false);
					Connector closeConnector2 = hypiping2.GetCloseConnector(curveIntersection, false);
					if (closeConnector != null && closeConnector2 != null && !closeConnector.IsConnected && !closeConnector2.IsConnected)
					{
						document.Create.NewElbowFitting(closeConnector, closeConnector2);
						flag = true;
					}
				}
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			return result;
		}

		public bool ConnectVerticalTerminal(FamilyInstance terminal1, FamilyInstance terminal2, Duct duct, ref string connMSG, ref List<Duct> connDucts)
		{
			bool result = true;
			Autodesk.Revit.DB.Document document = duct.Document;
			ConnVerticalTerminal connVerticalTerminal = new ConnVerticalTerminal();
			ConnVerticalTerminal connVerticalTerminal2 = new ConnVerticalTerminal();
			Connector connector = null;
			Connector connector2 = null;
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			SubTransaction subTransaction = new SubTransaction(document);
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			IList<Duct> ducts = new List<Duct>();
			IList<XYZ> xyzs = new List<XYZ>();
			List<string> listName = new List<string>();
			try
			{
				subTransaction.Start();
				if (terminal1 != null)
				{
					connector = YJKRevitTools.GetElementConnector((Domain)1, terminal1);
				}
				if (terminal2 != null)
				{
					connector2 = YJKRevitTools.GetElementConnector((Domain)1, terminal2);
				}
				connDucts = new List<Duct>();
				if (connector == null || connector2 == null)
				{
					return false;
				}
				connVerticalTerminal = this.CheckRelatePosition(terminal1, duct);
				connVerticalTerminal2 = this.CheckRelatePosition(terminal2, duct);
				if (Math.Abs(connVerticalTerminal.Connector.Origin.Z - connVerticalTerminal2.Connector.Origin.Z) > 0.016404199475065617)
				{
					connMSG = "两风口标高应该一致。";
					return false;
				}
				if (connVerticalTerminal.PositionType < 200 || connVerticalTerminal2.PositionType < 200)
				{
					connMSG = "风口连接口朝向不正确，\n四通连接仅支持与风管垂直关系的风口。";
					return false;
				}
				if (connVerticalTerminal.PointOnDuct.DistanceTo(connVerticalTerminal2.PointOnDuct) <= 0.00984251968503937)
				{
					if ((connVerticalTerminal.PositionType == 210 || connVerticalTerminal.PositionType == 211) && (connVerticalTerminal2.PositionType == 210 || connVerticalTerminal2.PositionType == 211))
					{
						if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, hypiping.GetDuctWidthOrHeight(0) / 3.0))
						{
							document.Create.NewTakeoffFitting(connVerticalTerminal2.Connector, hypiping.Piping);
							document.Create.NewTakeoffFitting(connVerticalTerminal.Connector, hypiping.Piping);
							result = true;
							document.Regenerate();
							connDucts.Add(duct);
						}
						else
						{
							this.GetConnDucts(hypiping.Piping, connVerticalTerminal.ConnDuct, connVerticalTerminal2.ConnDuct, ref ducts, ref xyzs, ref listName, 1);
							this.NewTeeFittingF(ducts, xyzs, listName, ref connMSG);
							if (!string.IsNullOrEmpty(connMSG))
							{
								return false;
							}
							result = true;
							document.Regenerate();
							connDucts.Add(duct);
						}
					}
					else if (connVerticalTerminal.PositionType == 212 && connVerticalTerminal2.PositionType == 212)
					{
						if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, hypiping.GetDuctWidthOrHeight(0) / 3.0))
						{
							this.GetConnDucts(hypiping.Piping, hypiping.Piping, connVerticalTerminal.ConnDuct, connVerticalTerminal2.ConnDuct, ref ducts, ref xyzs, ref listName, 2);
							this.NewCrossFitting(ducts, xyzs, listName, ref connMSG);
							result = true;
							document.Regenerate();
							connDucts.Add(duct);
						}
						else
						{
							this.GetConnDucts(hypiping.Piping, connVerticalTerminal.ConnDuct, connVerticalTerminal2.ConnDuct, ref ducts, ref xyzs, ref listName, 1);
							this.NewTeeFittingF(ducts, xyzs, listName, ref connMSG);
							if (!string.IsNullOrEmpty(connMSG))
							{
								return false;
							}
							result = true;
							document.Regenerate();
							connDucts.Add(duct);
						}
					}
					else if ((connVerticalTerminal.PositionType == 213 && connVerticalTerminal2.PositionType == 213) || (connVerticalTerminal.PositionType == 214 && connVerticalTerminal2.PositionType == 214) || (connVerticalTerminal.PositionType == 215 && connVerticalTerminal2.PositionType == 215) || (connVerticalTerminal.PositionType == 218 && connVerticalTerminal2.PositionType == 218))
					{
						Duct duct2 = YJKRevitTools.NewDuct(duct, connVerticalTerminal.PointOnFace, connVerticalTerminal.PointOnDuct);
						this.DuctSetParameter(duct2, connVerticalTerminal.Connector);
						hyrotateFamilyInstance.MovePiping(duct2);
						document.Regenerate();
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
						if (hypiping2.Shape == ConnectorSharp.Round)
						{
							this.GetConnDucts(connVerticalTerminal.ConnDuct, connVerticalTerminal2.ConnDuct, hypiping2.Piping, ref ducts, ref xyzs, ref listName, 0);
							this.NewTeeFittingHT(ducts, xyzs, listName, ref connMSG);
							if (!string.IsNullOrEmpty(connMSG))
							{
								return false;
							}
						}
						if (hypiping2.Shape == ConnectorSharp.Rect)
						{
							this.GetConnDucts(hypiping2.Piping, connVerticalTerminal.ConnDuct, connVerticalTerminal2.ConnDuct, ref ducts, ref xyzs, ref listName, 1);
							this.NewTeeFittingHF(ducts, xyzs, listName, ref connMSG);
							if (!string.IsNullOrEmpty(connMSG))
							{
								return false;
							}
						}
						if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, hypiping.GetDuctWidthOrHeight(0) / 3.0))
						{
							if (document.Create.NewTakeoffFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnDuct, false), hypiping2.Piping) == null)
							{
								connMSG = "风口距离风管垂直距离太短，\n无法完成连接。";
								return false;
							}
						}
						else if (document.Create.NewElbowFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnDuct, false), hypiping.GetCloseConnector(connVerticalTerminal.PointOnDuct, false)) == null)
						{
							connMSG = "风口距离风管垂直距离太短，\n无法完成连接。";
							return false;
						}
						document.Regenerate();
						connDucts.Add(duct);
					}
					else
					{
						if (connVerticalTerminal.PositionType != 216 || connVerticalTerminal2.PositionType != 216)
						{
							connMSG = "风口位置应大致对称，\n请调节后在进行四通连接操作。";
							return false;
						}
						Duct duct2 = YJKRevitTools.NewDuct(duct, connVerticalTerminal.PointOnFace, connVerticalTerminal.PointOnDuct);
						this.DuctSetParameter(duct2, connVerticalTerminal.Connector);
						hyrotateFamilyInstance.MovePiping(duct2);
						document.Regenerate();
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
						if (hypiping2.Shape == ConnectorSharp.Round)
						{
							this.GetConnDucts(connVerticalTerminal.ConnDuct, connVerticalTerminal2.ConnDuct, hypiping2.Piping, ref ducts, ref xyzs, ref listName, 0);
							this.NewTeeFittingHT(ducts, xyzs, listName, ref connMSG);
							if (!string.IsNullOrEmpty(connMSG))
							{
								return false;
							}
						}
						if (hypiping2.Shape == ConnectorSharp.Rect)
						{
							this.GetConnDucts(hypiping2.Piping, connVerticalTerminal.ConnDuct, connVerticalTerminal2.ConnDuct, ref ducts, ref xyzs, ref listName, 1);
							this.NewTeeFittingHF(ducts, xyzs, listName, ref connMSG);
							if (!string.IsNullOrEmpty(connMSG))
							{
								return false;
							}
						}
						if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, hypiping.GetDuctWidthOrHeight(0) / 3.0))
						{
							this.GetConnDucts(hypiping.Piping, hypiping.Piping, hypiping2.Piping, ref ducts, ref xyzs, ref listName, 0);
							this.NewTeeFittingHT(ducts, xyzs, listName, ref connMSG);
							if (!string.IsNullOrEmpty(connMSG))
							{
								return false;
							}
						}
						else if (document.Create.NewElbowFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnDuct, false), hypiping.GetCloseConnector(connVerticalTerminal.PointOnDuct, false)) == null)
						{
							connMSG = "风管管件配置中缺少弯头连接件配置，\n无法完成连接。";
							return false;
						}
						document.Regenerate();
						connDucts.Add(duct);
					}
				}
				else
				{
					this.ConnectVerticalTerminal(terminal1, duct, ref connMSG, ref connDucts);
					Duct duct3 = null;
					if (connDucts != null && connDucts.Count > 0)
					{
						if (connDucts.Count == 1)
						{
							duct3 = connDucts[0];
						}
						else if (connDucts.Count == 2)
						{
							YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(connDucts[0]);
							YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(connDucts[1]);
							XYZ xyz = hypiping3.StartConnector.Origin.Add(hypiping3.EndConnector.Origin) / 2.0;
							XYZ xyz2 = hypiping4.StartConnector.Origin.Add(hypiping4.EndConnector.Origin) / 2.0;
							duct3 = ((xyz.DistanceTo(YJKRevitTools.GetElementConnector((Domain)1, terminal2).Origin) > xyz2.DistanceTo(YJKRevitTools.GetElementConnector((Domain)1, terminal2).Origin)) ? connDucts[1] : connDucts[0]);
						}
					}
					if (duct3 == null)
					{
						connMSG = "风口位置不正确，\n请调节后在进行连接操作。";
						return false;
					}
					this.ConnectVerticalTerminal(terminal2, duct3, ref connMSG, ref connDucts);
				}
				subTransaction.Commit();
			}
			catch (Exception)
			{
				connMSG = "未连接成功，可能原因是：1、风管管件配置缺少相应连接件配置；\n2、风口位置距离风管太近。";
				result = false;
				return result;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool ConnectVerticalTerminal(FamilyInstance terminal1, Duct duct, ref string connMSG, ref List<Duct> connDucts)
		{
			bool result = true;
			Autodesk.Revit.DB.Document document = duct.Document;
			ConnVerticalTerminal connVerticalTerminal = new ConnVerticalTerminal();
			Connector connector = null;
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			SubTransaction subTransaction = new SubTransaction(document);
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			Duct duct2 = duct;
			IList<Duct> list = new List<Duct>();
			IList<XYZ> xyzs = new List<XYZ>();
			List<string> listName = new List<string>();
			try
			{
				subTransaction.Start();
				if (terminal1 != null)
				{
					connector = YJKRevitTools.GetElementConnector((Domain)1, terminal1);
				}
				connDucts = new List<Duct>();
				if (connector == null)
				{
					return false;
				}
				connVerticalTerminal = this.CheckRelatePosition(terminal1, duct);
				int positionType = connVerticalTerminal.PositionType;
				if (positionType <= 0)
				{
					if (positionType != -1)
					{
						if (positionType == 0)
						{
							connMSG = "风管带有坡度，\n无法进行连接操作！";
							result = false;
						}
					}
					else
					{
						connMSG = "风管、风口有空实例，\n或者风管为立管，\n无法进行连接操作！";
						result = false;
					}
				}
				else
				{
					switch (positionType)
					{
					case 100:
						connMSG = "风口与其相近的风管端点连接口方向相反，\n无法进行连接操作！";
						result = false;
						break;
					case 101:
					case 102:
					case 103:
					case 104:
					case 105:
					case 106:
					case 107:
					case 108:
					case 109:
					case 110:
						break;
					case 111:
						YJKRevitTools.ChangeDuctLine(ref duct2, connVerticalTerminal.PointOnDuct);
						hypiping = new YJKPiping<Duct>(duct2);
						hypiping.GetCloseConnector(connVerticalTerminal.PointOnDuct, false).ConnectTo(connVerticalTerminal.Connector);
						connDucts.Add(duct2);
						document.Regenerate();
						result = true;
						break;
					case 112:
					case 115:
					case 116:
					case 118:
						YJKRevitTools.ChangeDuctLine(ref duct2, connVerticalTerminal.PointOnDuct);
						hypiping = new YJKPiping<Duct>(duct2);
						document.Create.NewTransitionFitting(connVerticalTerminal.Connector, hypiping.GetCloseConnector(connVerticalTerminal.Connector.Origin, false));
						connDucts.Add(duct2);
						result = true;
						break;
					case 113:
					case 117:
					case 119:
					case 120:
						connMSG = "风口与风管间的位置不足，\n无法进行连接操作！";
						result = false;
						break;
					case 114:
					{
						Duct duct3 = YJKRevitTools.NewDuct(duct, connVerticalTerminal.Connector.Origin, connVerticalTerminal.PointOnDuct);
						this.DuctSetParameter(duct3, connVerticalTerminal.Connector);
						hyrotateFamilyInstance.MovePiping(duct3);
						document.Regenerate();
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
						document.Create.NewElbowFitting(hypiping.GetCloseConnector(connVerticalTerminal.Connector.Origin, false), hypiping2.GetCloseConnector(connVerticalTerminal.PointOnDuct, false));
						document.Create.NewElbowFitting(hypiping2.GetCloseConnector(connVerticalTerminal.Connector.Origin, false), connVerticalTerminal.Connector);
						connDucts.Add(duct2);
						document.Regenerate();
						result = true;
						break;
					}
					case 121:
					{
						Duct duct3 = YJKRevitTools.NewDuct(duct, connVerticalTerminal.PointOnDuct, connVerticalTerminal.PointOnFace);
						this.DuctSetParameter(duct3, connVerticalTerminal.Connector);
						hyrotateFamilyInstance.MovePiping(duct3);
						document.Regenerate();
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
						document.Create.NewElbowFitting(hypiping.GetCloseConnector(connVerticalTerminal.Connector.Origin, false), hypiping2.GetCloseConnector(connVerticalTerminal.PointOnDuct, false));
						Duct duct4 = YJKRevitTools.NewDuct(duct, connVerticalTerminal.Connector.Origin, connVerticalTerminal.PointOnFace);
						this.DuctSetParameter(duct4, connVerticalTerminal.Connector);
						hyrotateFamilyInstance.MovePiping(duct4);
						document.Regenerate();
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct4);
						document.Create.NewElbowFitting(hypiping3.GetCloseConnector(connVerticalTerminal.PointOnFace, false), hypiping2.GetCloseConnector(connVerticalTerminal.PointOnFace, false));
						document.Create.NewElbowFitting(hypiping3.GetCloseConnector(connVerticalTerminal.Connector.Origin, false), connVerticalTerminal.Connector);
						document.Regenerate();
						connDucts.Add(duct2);
						result = true;
						break;
					}
					case 122:
						list = new List<Duct>();
						hyrotateFamilyInstance.MovePiping(connVerticalTerminal.ConnDuct);
						list.Add(connVerticalTerminal.ConnDuct);
						list.Add(duct2);
						this.NewLHWFitting(list, true, this.GetElbowByDuct(duct2), ref connMSG);
						document.Regenerate();
						connDucts.Add(duct2);
						result = true;
						break;
					default:
						switch (positionType)
						{
						case 200:
							connMSG = "风口连接口背对风管，\n无法进行连接操作！";
							result = false;
							break;
						case 211:
							if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, hypiping.GetDuctWidthOrHeight(0) / 3.0))
							{
								document.Create.NewTakeoffFitting(connVerticalTerminal.Connector, hypiping.Piping);
								result = true;
								document.Regenerate();
								connDucts.Add(duct2);
							}
							else
							{
								connMSG = "风口距离风管端点太近，\n无法进行连接！";
								result = false;
							}
							break;
						case 212:
							if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, hypiping.GetDuctWidthOrHeight(0) / 3.0))
							{
								this.GetConnDucts(hypiping.Piping, hypiping.Piping, connVerticalTerminal.ConnDuct, ref list, ref xyzs, ref listName, 0);
								this.NewTeeFittingT(list, xyzs, listName, ref connMSG);
								if (string.IsNullOrEmpty(connMSG))
								{
									result = true;
									document.Regenerate();
								}
								else
								{
									result = false;
								}
								connDucts.Add(list[0]);
								connDucts.Add(list[1]);
							}
							else
							{
								document.Create.NewElbowFitting(connVerticalTerminal.Connector, hypiping.GetCloseConnector(connVerticalTerminal.PointOnDuct, false));
								result = true;
								document.Regenerate();
								connDucts.Add(duct2);
							}
							break;
						case 213:
						case 218:
							if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, hypiping.GetDuctWidthOrHeight(0) / 3.0))
							{
								XYZ xyz = new XYZ(connVerticalTerminal.Connector.Origin.X, connVerticalTerminal.Connector.Origin.Y, connVerticalTerminal.PointOnDuct.Z);
								Duct duct3 = YJKRevitTools.NewDuct(duct, xyz, connVerticalTerminal.PointOnDuct);
								this.DuctSetParameter(duct3, connVerticalTerminal.Connector);
								hyrotateFamilyInstance.MovePiping(duct3);
								document.Regenerate();
								YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
								document.Create.NewTakeoffFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnDuct, false), hypiping.Piping);
								document.Create.NewTransitionFitting(hypiping2.GetCloseConnector(xyz, false), connVerticalTerminal.Connector);
								result = true;
								document.Regenerate();
								connDucts.Add(duct2);
							}
							else
							{
								connMSG = "风口距离风管端点太近，\n无法进行连接！";
								result = false;
							}
							break;
						case 214:
						case 215:
							if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, hypiping.GetDuctWidthOrHeight(0) / 3.0))
							{
								Duct duct3 = YJKRevitTools.NewDuct(duct, connVerticalTerminal.PointOnFace, connVerticalTerminal.PointOnDuct);
								this.DuctSetParameter(duct3, connVerticalTerminal.Connector);
								hyrotateFamilyInstance.MovePiping(duct3);
								document.Regenerate();
								YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
								document.Create.NewTakeoffFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnDuct, false), hypiping.Piping);
								document.Create.NewElbowFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnFace, false), connVerticalTerminal.Connector);
								result = true;
								document.Regenerate();
								connDucts.Add(duct2);
							}
							else if (connVerticalTerminal.PositionType == 215)
							{
								Duct duct3 = YJKRevitTools.NewDuct(duct, connVerticalTerminal.PointOnFace, connVerticalTerminal.PointOnDuct);
								this.DuctSetParameter(duct3, connVerticalTerminal.Connector);
								hyrotateFamilyInstance.MovePiping(duct3);
								document.Regenerate();
								YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
								document.Create.NewElbowFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnDuct, false), hypiping.GetCloseConnector(connVerticalTerminal.PointOnFace, false));
								document.Create.NewElbowFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnFace, false), connVerticalTerminal.Connector);
								result = true;
								document.Regenerate();
								connDucts.Add(duct2);
							}
							else
							{
								connMSG = "风口距离风管端点太近，\n无法进行连接！";
								result = false;
							}
							break;
						case 216:
							if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, hypiping.GetDuctWidthOrHeight(0) / 3.0))
							{
								Duct duct3 = YJKRevitTools.NewDuct(duct, connVerticalTerminal.PointOnFace, connVerticalTerminal.PointOnDuct);
								this.DuctSetParameter(duct3, connVerticalTerminal.Connector);
								hyrotateFamilyInstance.MovePiping(duct3);
								document.Regenerate();
								YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
								document.Create.NewElbowFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnFace, false), connVerticalTerminal.Connector);
								result = true;
								this.GetConnDucts(hypiping.Piping, hypiping.Piping, duct3, ref list, ref xyzs, ref listName, 0);
								this.NewTeeFittingHT(list, xyzs, listName, ref connMSG);
								if (string.IsNullOrEmpty(connMSG))
								{
									result = true;
									document.Regenerate();
									connDucts.Add(list[0]);
									connDucts.Add(list[1]);
								}
								else
								{
									result = false;
								}
								document.Regenerate();
							}
							else
							{
								Duct duct3 = YJKRevitTools.NewDuct(duct, connVerticalTerminal.PointOnFace, connVerticalTerminal.PointOnDuct);
								this.DuctSetParameter(duct3, connVerticalTerminal.Connector);
								hyrotateFamilyInstance.MovePiping(duct3);
								document.Regenerate();
								YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
								document.Create.NewElbowFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnDuct, false), hypiping.GetCloseConnector(connVerticalTerminal.PointOnFace, false));
								document.Create.NewElbowFitting(hypiping2.GetCloseConnector(connVerticalTerminal.PointOnFace, false), connVerticalTerminal.Connector);
								result = true;
								document.Regenerate();
								connDucts.Add(duct2);
							}
							break;
						case 217:
						case 220:
							document.DeleteElement(connVerticalTerminal.ConnDuct);
							this.DuctConnectAirTerminalTakeOff(duct2, terminal1, ref connMSG);
							connDucts.Add(duct2);
							break;
						case 219:
							connMSG = "风口与风管间距太小，\n无法进行连接操作！";
							result = false;
							break;
						}
						break;
					}
				}
				subTransaction.Commit();
			}
			catch (Exception)
			{
				connMSG = "未连接成功，可能原因是：\n1、风管管件配置缺少相应连接件配置；\n2、风口位置距离风管太近。";
				result = false;
				return result;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool ConnectVerticalTerminal(Duct duct, FamilyInstance terminal1, FamilyInstance terminal2, ref string connMSG)
		{
			bool result = true;
			Autodesk.Revit.DB.Document document = duct.Document;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			Connector connector = null;
			Connector connector2 = null;
			ConnVerticalTerminal connVerticalTerminal = new ConnVerticalTerminal();
			new ConnVerticalTerminal();
			SubTransaction subTransaction = new SubTransaction(document);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			Duct duct2 = null;
			try
			{
				subTransaction.Start();
				if (terminal1 != null)
				{
					connector = YJKRevitTools.GetElementConnector((Domain)1, terminal1);
				}
				if (terminal2 != null)
				{
					connector2 = YJKRevitTools.GetElementConnector((Domain)1, terminal2);
				}
				if (connector != null && connector2 == null)
				{
					connVerticalTerminal = this.CheckRelatePosition(duct, terminal1);
					if (connVerticalTerminal.PositionType == -1)
					{
						connMSG = "风管、风口有空实例，\n或者风管为立管，\n无法进行连接操作！";
						return false;
					}
					if (connVerticalTerminal.PositionType == 0)
					{
						connMSG = "风管带有坡度，\n无法进行连接操作！";
						return false;
					}
					if (connVerticalTerminal.PositionType == 11)
					{
						Connector connector3 = this.CreatTerminalDuct(duct, connVerticalTerminal.Connector, ref duct2);
						Connector closeConnector = hypiping.GetCloseConnector(connVerticalTerminal.Connector.Origin, false);
						closeConnector.ConnectTo(connector3);
						result = true;
						document.Regenerate();
						hyrotateFamilyInstance.MoveFamilyInstance(terminal1, connVerticalTerminal.Connector.CoordinateSystem.BasisX);
					}
					else if (connVerticalTerminal.PositionType == 12)
					{
						Connector connector3 = this.CreatTerminalDuct(duct, connVerticalTerminal.Connector, ref duct2);
						Connector closeConnector = hypiping.GetCloseConnector(connVerticalTerminal.Connector.Origin, false);
						document.Create.NewTransitionFitting(connector3, closeConnector);
						result = true;
						document.Regenerate();
					}
					else if (connVerticalTerminal.PositionType == 13)
					{
						Connector connector3 = this.CreatTerminalDuct(duct, connVerticalTerminal.Connector, ref duct2);
						Connector closeConnector = hypiping.GetCloseConnector(connVerticalTerminal.Connector.Origin, false);
						Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, connVerticalTerminal.PointOnFace, connVerticalTerminal.Connector.Origin);
						this.DuctSetParameter(connVerticalTerminal.Connector, duct3, false);
						Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, connVerticalTerminal.PointOnFace, connVerticalTerminal.PointOnDuct);
						this.DuctSetParameter(connector3, duct4, false);
						hyrotateFamilyInstance.MovePiping(duct3);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct4);
						Connector closeConnector2 = hypiping2.GetCloseConnector(connVerticalTerminal.PointOnFace, false);
						Connector closeConnector3 = hypiping3.GetCloseConnector(connVerticalTerminal.PointOnFace, false);
						document.Create.NewElbowFitting(closeConnector2, closeConnector3);
						closeConnector3 = hypiping3.GetCloseConnector(connVerticalTerminal.PointOnDuct, false);
						document.Create.NewElbowFitting(closeConnector, closeConnector3);
						result = true;
						document.Regenerate();
					}
					else
					{
						if (connVerticalTerminal.PositionType == 10 || connVerticalTerminal.PositionType == 29)
						{
							connMSG = "侧风口连接件朝向有误，\n无法进行连接操作！";
							return false;
						}
						if (connVerticalTerminal.PositionType == 20 || connVerticalTerminal.PositionType == 27 || connVerticalTerminal.PositionType == 28)
						{
							connMSG = "侧风口距离风管太近，\n无法进行连接操作！";
							return false;
						}
						if (connVerticalTerminal.PositionType == 21)
						{
							XYZ xyz = connVerticalTerminal.Connector.Origin + connVerticalTerminal.Connector.CoordinateSystem.BasisZ * 50.0 / 304.8;
							Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, xyz, connVerticalTerminal.Connector.Origin);
							this.DuctSetParameter(connVerticalTerminal.Connector, duct3, false);
							hyrotateFamilyInstance.MovePiping(duct3);
							YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct3);
							if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, 1.6404199475065617))
							{
								document.Create.NewTakeoffFitting(hypiping4.GetCloseConnector(xyz, false), hypiping.Piping);
								result = true;
								document.Regenerate();
							}
							else
							{
								Connector closeConnector = hypiping.GetCloseConnector(xyz, false);
								Connector closeConnector4 = hypiping4.GetCloseConnector(xyz, false);
								document.Create.NewElbowFitting(closeConnector, closeConnector4);
								result = true;
								document.Regenerate();
							}
						}
						else if (connVerticalTerminal.PositionType == 23)
						{
							XYZ xyz2 = connVerticalTerminal.Connector.Origin + connVerticalTerminal.Connector.CoordinateSystem.BasisZ * 50.0 / 304.8;
							Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, xyz2, connVerticalTerminal.Connector.Origin);
							this.DuctSetParameter(connVerticalTerminal.Connector, duct3, false);
							hyrotateFamilyInstance.MovePiping(duct3);
							YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct3);
							if (hypiping.IsPointOnPipingCurve(connVerticalTerminal.PointOnDuct, 1.6404199475065617))
							{
								IList<Duct> list = new List<Duct>();
								list.Add(duct);
								list.Add(duct);
								list.Add(duct3);
								IList<XYZ> list2 = new List<XYZ>();
								list2.Add(hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0);
								list2.Add(hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0);
								list2.Add(hypiping5.StartConnector.Origin.Add(hypiping5.EndConnector.Origin) / 2.0);
								List<string> list3 = new List<string>();
                                if ((int)hypiping.Shape == 1)
								{
									list3.Add("矩形圆角三通-顶对齐");
									list3.Add("矩形圆角三通-中心对齐");
									list3.Add("矩形圆角三通-底对齐");
								}
								else
								{
									if (hypiping.Shape != null)
									{
										throw new Exception("目前不支持椭圆形风管及风口连接！");
									}
									list3.Add("圆形斜角三通-顶对齐");
									list3.Add("圆形斜角三通-中心对齐");
									list3.Add("圆形斜角三通-底对齐");
								}
								this.NewTeeFittingT(list, list2, list3, ref connMSG);
								result = string.IsNullOrEmpty(connMSG);
								document.Regenerate();
							}
							else
							{
								Connector closeConnector = hypiping.GetCloseConnector(xyz2, false);
								Connector closeConnector5 = hypiping5.GetCloseConnector(xyz2, false);
								document.Create.NewElbowFitting(closeConnector, closeConnector5);
								result = true;
								document.Regenerate();
							}
						}
						else if (connVerticalTerminal.PositionType == 24)
						{
							XYZ xyz3 = connVerticalTerminal.PointOnDuct + hypiping.GetWeightOrDiameter() / 2.0 * connVerticalTerminal.Connector.CoordinateSystem.BasisZ;
							Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, xyz3, connVerticalTerminal.Connector.Origin);
							this.DuctSetParameter(connVerticalTerminal.Connector, duct3, false);
							XYZ endXYZ = connVerticalTerminal.PointOnDuct - hypiping.GetWeightOrDiameter() / 2.0 * connVerticalTerminal.Connector.CoordinateSystem.BasisZ;
							Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, connVerticalTerminal.PointOnDuct, endXYZ);
							this.DuctSetParameter(connVerticalTerminal.Connector, duct4, false);
							hyrotateFamilyInstance.MovePiping(duct3);
							hyrotateFamilyInstance.MovePiping(duct4);
							YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(duct4);
							IList<Duct> list4 = new List<Duct>();
							list4.Add(duct);
							list4.Add(duct);
							list4.Add(duct3);
							IList<XYZ> list5 = new List<XYZ>();
							list5.Add(hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0);
							list5.Add(hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0);
							list5.Add(hypiping6.StartConnector.Origin.Add(hypiping6.EndConnector.Origin) / 2.0);
							List<string> list6 = new List<string>();
							if ((int)hypiping.Shape == 1)
							{
								list6.Add("矩形圆角三通-顶对齐");
								list6.Add("矩形圆角三通-中心对齐");
								list6.Add("矩形圆角三通-底对齐");
							}
							else
							{
								if (hypiping.Shape != null)
								{
									throw new Exception("目前不支持椭圆形风管及风口连接！");
								}
								list6.Add("圆形斜角三通-顶对齐");
								list6.Add("圆形斜角三通-中心对齐");
								list6.Add("圆形斜角三通-底对齐");
							}
							this.NewTeeFittingT(list4, list5, list6, ref connMSG);
							result = string.IsNullOrEmpty(connMSG);
							document.Regenerate();
							YJKPiping<Duct> hypiping7 = new YJKPiping<Duct>(duct3);
							document.Create.NewTransitionFitting(hypiping7.GetCloseConnector(xyz3, false), hypiping6.GetCloseConnector(xyz3, false));
							document.Regenerate();
						}
						else if (connVerticalTerminal.PositionType == 25)
						{
							XYZ xyz4 = connVerticalTerminal.PointOnDuct + hypiping.GetWeightOrDiameter() / 2.0 * connVerticalTerminal.Connector.CoordinateSystem.BasisZ;
							Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, xyz4, connVerticalTerminal.Connector.Origin);
							this.DuctSetParameter(connVerticalTerminal.Connector, duct3, false);
							XYZ endXYZ2 = connVerticalTerminal.PointOnDuct - hypiping.GetWeightOrDiameter() / 2.0 * connVerticalTerminal.PointOnFace.Subtract(connVerticalTerminal.PointOnDuct).Normalize();
							Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, connVerticalTerminal.PointOnDuct, endXYZ2);
							this.DuctSetParameter(connVerticalTerminal.Connector, duct4, false);
							hyrotateFamilyInstance.MovePiping(duct3);
							hyrotateFamilyInstance.MovePiping(duct4);
							YJKPiping<Duct> hypiping8 = new YJKPiping<Duct>(duct4);
							IList<Duct> list7 = new List<Duct>();
							list7.Add(duct);
							list7.Add(duct);
							list7.Add(duct3);
							IList<XYZ> list8 = new List<XYZ>();
							list8.Add(hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0);
							list8.Add(hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0);
							list8.Add(hypiping8.StartConnector.Origin.Add(hypiping8.EndConnector.Origin) / 2.0);
							List<string> list9 = new List<string>();
							if ((int)hypiping.Shape == 1)
							{
								list9.Add("矩形圆角三通-顶对齐");
								list9.Add("矩形圆角三通-中心对齐");
								list9.Add("矩形圆角三通-底对齐");
							}
							else
							{
								if (hypiping.Shape != null)
								{
									throw new Exception("目前不支持椭圆形风管及风口连接！");
								}
								list9.Add("圆形斜角三通-顶对齐");
								list9.Add("圆形斜角三通-中心对齐");
								list9.Add("圆形斜角三通-底对齐");
							}
							this.NewTeeFittingHT(list7, list8, list9, ref connMSG);
							result = string.IsNullOrEmpty(connMSG);
							document.Regenerate();
							YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(duct3);
							document.Create.NewElbowFitting(hypiping9.GetCloseConnector(xyz4, false), hypiping8.GetCloseConnector(xyz4, false));
							document.Regenerate();
						}
					}
				}
				else if (connector == null || connector2 == null)
				{
					return false;
				}
				subTransaction.Commit();
			}
			catch (Exception)
			{
				result = false;
				return result;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private FamilySymbol GetElbowByDuct(Duct duct)
		{
			FamilySymbol result = null;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			string text = "";
			ConnectDuctManage connectDuctManage = new ConnectDuctManage();
			if (hypiping.Shape == ConnectorSharp.Rect)
			{
				result = connectDuctManage.GetFamilySymbol("内外弧形矩形弯头", duct.Document, ref text);
			}
			if (hypiping.Shape == ConnectorSharp.Round)
			{
				result = connectDuctManage.GetFamilySymbol("圆形弯头", duct.Document, ref text);
			}
			return result;
		}

		private void GetConnDucts(Duct duct1, Duct duct2, Duct duct3, ref IList<Duct> listDuct, ref IList<XYZ> listPnt, ref List<string> listName, int connType = 0)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct1);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
			listDuct = new List<Duct>();
			listDuct.Add(duct1);
			listDuct.Add(duct2);
			listDuct.Add(duct3);
			listPnt = new List<XYZ>();
			listPnt.Add(hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0);
			listPnt.Add(hypiping2.StartConnector.Origin.Add(hypiping2.EndConnector.Origin) / 2.0);
			listPnt.Add(hypiping3.StartConnector.Origin.Add(hypiping3.EndConnector.Origin) / 2.0);
			if (connType == 1)
			{
				listName = new List<string>();
				ConnectorProfileType shape = hypiping.Shape;
				ConnectorProfileType round = ConnectorSharp.Round;
				if (hypiping.Shape == ConnectorSharp.Rect)
				{
					listName.Add("矩形Y型三通-顶对齐");
					listName.Add("矩形Y型三通-中心对齐");
					listName.Add("矩形Y型三通-底对齐");
					return;
				}
			}
			else
			{
				if (hypiping.Shape == ConnectorSharp.Round)
				{
					listName.Add("圆形直角三通-顶对齐");
					listName.Add("圆形直角三通-中心对齐");
					listName.Add("圆形直角三通-底对齐");
				}
				if (hypiping.Shape == ConnectorSharp.Rect)
				{
					listName = new List<string>();
					listName.Add("矩形圆角三通-顶对齐");
					listName.Add("矩形圆角三通-中心对齐");
					listName.Add("矩形圆角三通-底对齐");
				}
			}
		}

		private void GetConnDucts(Duct duct1, Duct duct2, Duct duct3, Duct duct4, ref IList<Duct> listDuct, ref IList<XYZ> listPnt, ref List<string> listName, int connType = 0)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct1);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
			YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct4);
			listDuct = new List<Duct>();
			listDuct.Add(duct1);
			listDuct.Add(duct2);
			listDuct.Add(duct3);
			listDuct.Add(duct4);
			listPnt = new List<XYZ>();
			listPnt.Add(hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0);
			listPnt.Add(hypiping2.StartConnector.Origin.Add(hypiping2.EndConnector.Origin) / 2.0);
			listPnt.Add(hypiping3.StartConnector.Origin.Add(hypiping3.EndConnector.Origin) / 2.0);
			listPnt.Add(hypiping4.StartConnector.Origin.Add(hypiping4.EndConnector.Origin) / 2.0);
			if (connType == 1)
			{
				listName = new List<string>();
				if (hypiping.Shape == ConnectorSharp.Round)
				{
					listName.Add("圆形直角三通-顶对齐");
					listName.Add("圆形直角三通-中心对齐");
					listName.Add("圆形直角三通-底对齐");
				}
				if (hypiping.Shape == ConnectorSharp.Rect)
				{
					listName.Add("矩形Y型三通-顶对齐");
					listName.Add("矩形Y型三通-中心对齐");
					listName.Add("矩形Y型三通-底对齐");
					return;
				}
			}
			else
			{
				if (hypiping.Shape == ConnectorSharp.Round)
				{
					listName.Add("圆形直角四通-顶对齐");
					listName.Add("圆形直角四通-中心对齐");
					listName.Add("圆形直角四通-底对齐");
				}
				if (hypiping.Shape == ConnectorSharp.Rect)
				{
					listName = new List<string>();
					listName.Add("矩形圆角四通-顶对齐");
					listName.Add("矩形圆角四通-中心对齐");
					listName.Add("矩形圆角四通-底对齐");
				}
			}
		}

		public ConnVerticalTerminal CheckRelatePosition(Duct duct, FamilyInstance terminal)
		{
			ConnVerticalTerminal connVerticalTerminal = new ConnVerticalTerminal();
			if (duct == null || terminal == null || YJKRevitTools.IsRiserDuct(duct))
			{
				connVerticalTerminal.PositionType = -1;
				return connVerticalTerminal;
			}
			Duct duct2 = null;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			YJKLine hyline = new YJKLine(origin, origin2);
			hyline.MakeUnBound();
			if (Math.Abs(origin2.Z - origin.Z) > 0.0001)
			{
				connVerticalTerminal.PositionType = 0;
				return connVerticalTerminal;
			}
			Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, terminal);
			this.CreatTerminalDuct(duct, elementConnector, ref duct2);
			connVerticalTerminal.ConnDuct = duct2;
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
			hypiping.GetDuctWidthOrHeight(0);
			hypiping.GetDuctWidthOrHeight(1);
			hypiping2.GetDuctWidthOrHeight(0);
			hypiping2.GetDuctWidthOrHeight(1);
			double distance = hyline.GetDistance(elementConnector.Origin);
			double weightOrDiameter = hypiping.GetWeightOrDiameter();
			Connector closeConnector = hypiping.GetCloseConnector(elementConnector.Origin, false);
			double num = 3.0 * weightOrDiameter;
			if (elementConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping.EndConnector.Direction) || elementConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping.StartConnector.Direction))
			{
				if (closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(elementConnector.CoordinateSystem.BasisZ * -1.0))
				{
					if (distance <= 0.016404199475065617)
					{
						connVerticalTerminal.PositionType = 11;
						connVerticalTerminal.Connector = elementConnector;
						connVerticalTerminal.PointOnDuct = closeConnector.Origin;
					}
					else if (distance <= num)
					{
						connVerticalTerminal.PositionType = 12;
						connVerticalTerminal.Connector = elementConnector;
						XYZ point = elementConnector.Origin + elementConnector.CoordinateSystem.BasisZ * 500.0 / 304.8;
						connVerticalTerminal.PointOnDuct = hyline.GetProject(point);
					}
					else if (distance > num)
					{
						connVerticalTerminal.PositionType = 13;
						connVerticalTerminal.Connector = elementConnector;
						XYZ xyz = elementConnector.Origin + elementConnector.CoordinateSystem.BasisZ * 500.0 / 304.8;
						connVerticalTerminal.PointOnFace = xyz;
						connVerticalTerminal.PointOnDuct = hyline.GetProject(xyz);
					}
				}
				else
				{
					connVerticalTerminal.PositionType = 10;
				}
			}
			double num2 = elementConnector.CoordinateSystem.BasisZ.AngleTo(hypiping.StartConnector.Direction);
			if (Math.Abs(num2 - Math.PI*.5) <= 0.0001 || Math.Abs(num2 - 4.71238898038469) <= 0.0001)
			{
				XYZ project = hyline.GetProject(elementConnector.Origin);
				if (!new XYZ(project.X, project.Y, elementConnector.Origin.Z).Subtract(elementConnector.Origin).Normalize().IsAlmostEqualTo(elementConnector.CoordinateSystem.BasisZ, 0.0001))
				{
					connVerticalTerminal.PositionType = 29;
					return connVerticalTerminal;
				}
				double num3 = Math.Abs(elementConnector.Origin.Z - hypiping.StartConnector.Origin.Z);
				if (num3 <= 0.016404199475065617)
				{
					connVerticalTerminal.Connector = elementConnector;
					connVerticalTerminal.PointOnDuct = project;
					if (distance - weightOrDiameter / 2.0 <= 0.001)
					{
						connVerticalTerminal.PositionType = 20;
					}
					else if (distance - weightOrDiameter / 2.0 < 1.6404199475065617)
					{
						connVerticalTerminal.PositionType = 21;
					}
					else
					{
						connVerticalTerminal.PositionType = 23;
					}
				}
				else if (num3 <= num)
				{
					connVerticalTerminal.PointOnFace = new XYZ(project.X, project.Y, elementConnector.Origin.Z);
					if (elementConnector.Origin.DistanceTo(connVerticalTerminal.PointOnFace) > num)
					{
						connVerticalTerminal.PositionType = 24;
					}
					else
					{
						connVerticalTerminal.PositionType = 28;
					}
					connVerticalTerminal.PointOnDuct = new XYZ(project.X, project.Y, hypiping.StartConnector.Origin.Z);
					connVerticalTerminal.Connector = elementConnector;
				}
				else if (num3 > num)
				{
					connVerticalTerminal.PointOnFace = new XYZ(project.X, project.Y, elementConnector.Origin.Z);
					if (elementConnector.Origin.DistanceTo(connVerticalTerminal.PointOnFace) > num)
					{
						connVerticalTerminal.PositionType = 25;
					}
					else
					{
						connVerticalTerminal.PositionType = 27;
					}
					connVerticalTerminal.PointOnDuct = new XYZ(project.X, project.Y, hypiping.StartConnector.Origin.Z);
					connVerticalTerminal.Connector = elementConnector;
				}
			}
			return connVerticalTerminal;
		}

		public ConnVerticalTerminal CheckRelatePosition(FamilyInstance terminal, Duct duct)
		{
			ConnVerticalTerminal connVerticalTerminal = new ConnVerticalTerminal();
			if (duct == null || terminal == null || YJKRevitTools.IsRiserDuct(duct))
			{
				connVerticalTerminal.PositionType = -1;
				return connVerticalTerminal;
			}
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			YJKLine hyline = new YJKLine(origin, origin2);
			hyline.MakeUnBound();
			if (Math.Abs(origin.Z - origin2.Z) > 0.0001)
			{
				connVerticalTerminal.PositionType = 0;
				return connVerticalTerminal;
			}
			Duct duct2 = null;
			Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, terminal);
			double ductWidthOrHeight = hypiping.GetDuctWidthOrHeight(0);
			double ductWidthOrHeight2 = hypiping.GetDuctWidthOrHeight(1);
			Connector closeConnector = hypiping.GetCloseConnector(elementConnector.Origin, false);
			Connector connector = this.CreatTerminalDuct(duct, elementConnector, ref duct2);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			new YJKLine(origin3, origin4).MakeUnBound();
			double ductWidthOrHeight3 = hypiping2.GetDuctWidthOrHeight(0);
			double ductWidthOrHeight4 = hypiping2.GetDuctWidthOrHeight(1);
			double distance = hyline.GetDistance(new XYZ(elementConnector.Origin.X, elementConnector.Origin.Y, origin.Z));
			double num = Math.Abs(elementConnector.Origin.Z - closeConnector.Origin.Z);
			double num2 = (ductWidthOrHeight > ductWidthOrHeight3) ? (ductWidthOrHeight3 * 2.0) : (ductWidthOrHeight * 2.0);
			double num3 = (ductWidthOrHeight2 > ductWidthOrHeight4) ? (ductWidthOrHeight4 * 2.0) : (ductWidthOrHeight2 * 2.0);
			connVerticalTerminal.ConnDuct = duct2;
			connVerticalTerminal.Connector = connector;
			if (elementConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping.EndConnector.Direction) || elementConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(hypiping.StartConnector.Direction))
			{
				if (closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(elementConnector.CoordinateSystem.BasisZ * -1.0))
				{
					connVerticalTerminal.PointOnDuct = hyline.GetProject(connector.Origin);
					connVerticalTerminal.PointOnFace = new XYZ(connVerticalTerminal.PointOnDuct.X, connVerticalTerminal.PointOnDuct.Y, elementConnector.Origin.Z);
					if (num <= 0.00984251968503937)
					{
						if (distance <= 0.00984251968503937)
						{
							connVerticalTerminal.PositionType = 111;
						}
						else if (distance <= num2)
						{
							connVerticalTerminal.PositionType = 112;
						}
						else if (distance <= num2 * 3.0)
						{
							connVerticalTerminal.PositionType = 113;
						}
						else
						{
							connVerticalTerminal.PositionType = 114;
						}
					}
					else if (num <= num3)
					{
						if (distance <= 0.00984251968503937)
						{
							connVerticalTerminal.PositionType = 115;
						}
						else if (distance <= num2)
						{
							connVerticalTerminal.PositionType = 116;
						}
						else
						{
							connVerticalTerminal.PositionType = 117;
						}
					}
					else if (num <= num3 * 2.0)
					{
						if (distance <= 0.00984251968503937)
						{
							connVerticalTerminal.PositionType = 122;
						}
						else
						{
							connVerticalTerminal.PositionType = 118;
						}
					}
					else if (distance <= 0.00984251968503937)
					{
						connVerticalTerminal.PositionType = 119;
					}
					else if (distance <= num2 * 2.0)
					{
						connVerticalTerminal.PositionType = 120;
					}
					else
					{
						connVerticalTerminal.PositionType = 121;
					}
				}
				else
				{
					connVerticalTerminal.PositionType = 100;
				}
			}
			double num4 = elementConnector.CoordinateSystem.BasisZ.AngleTo(hypiping.StartConnector.Direction);
			if (Math.Abs(num4 - Math.PI*.5) <= 0.0001 || Math.Abs(num4 - 4.71238898038469) <= 0.0001)
			{
				XYZ project = hyline.GetProject(elementConnector.Origin);
				XYZ xyz = new XYZ(project.X, project.Y, elementConnector.Origin.Z).Subtract(elementConnector.Origin).Normalize();
				connVerticalTerminal.PointOnDuct = hyline.GetProject(connector.Origin);
				connVerticalTerminal.PointOnFace = new XYZ(connVerticalTerminal.PointOnDuct.X, connVerticalTerminal.PointOnDuct.Y, elementConnector.Origin.Z);
				if (!xyz.IsAlmostEqualTo(elementConnector.CoordinateSystem.BasisZ, 0.0001))
				{
					connVerticalTerminal.PositionType = 200;
					return connVerticalTerminal;
				}
				if (num <= 0.00984251968503937)
				{
					if (distance <= ductWidthOrHeight / 2.0 * 0.05)
					{
						connVerticalTerminal.PositionType = 217;
					}
					if (distance - num2 < 0.00984251968503937)
					{
						connVerticalTerminal.PositionType = 220;
					}
					else if (distance <= num2 * 2.0)
					{
						connVerticalTerminal.PositionType = 211;
					}
					else
					{
						connVerticalTerminal.PositionType = 212;
					}
				}
				else if (num <= num3)
				{
					if (distance <= ductWidthOrHeight / 2.0 * 0.05)
					{
						connVerticalTerminal.PositionType = 219;
					}
					if (distance - num2 < 0.00984251968503937)
					{
						connVerticalTerminal.PositionType = 213;
					}
					else if (distance <= num2 * 2.0)
					{
						connVerticalTerminal.PositionType = 213;
					}
					else
					{
						connVerticalTerminal.PositionType = 213;
					}
				}
				else if (num <= num3 * 2.0)
				{
					if (distance <= ductWidthOrHeight / 2.0 * 0.05)
					{
						connVerticalTerminal.PositionType = 217;
					}
					else
					{
						connVerticalTerminal.PositionType = 218;
					}
				}
				else
				{
					if (distance <= ductWidthOrHeight / 2.0 * 0.05)
					{
						connVerticalTerminal.PositionType = 219;
					}
					if (distance - num2 < 0.00984251968503937)
					{
						connVerticalTerminal.PositionType = 214;
					}
					else if (distance <= num2 * 2.0)
					{
						connVerticalTerminal.PositionType = 215;
					}
					else
					{
						connVerticalTerminal.PositionType = 216;
					}
				}
			}
			return connVerticalTerminal;
		}

		private void ConnNewDuctAndConnector(Connector conn, Duct newDuct)
		{
			new YJKPiping<Duct>(newDuct).GetNearConnectorToPoint(conn.Origin).ConnectTo(conn);
		}

		public Connector CreatTerminalDuct(Duct duct, Connector conn, ref Duct createdDuct)
		{
			Autodesk.Revit.DB.Document document = duct.Document;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			double ductWidthOrHeight = hypiping.GetDuctWidthOrHeight(0);
			double ductWidthOrHeight2 = hypiping.GetDuctWidthOrHeight(1);
			double connWidthOrHeight = this.GetConnWidthOrHeight(0, conn);
			double connWidthOrHeight2 = this.GetConnWidthOrHeight(1, conn);
			if (ductWidthOrHeight > ductWidthOrHeight2 && connWidthOrHeight < connWidthOrHeight2)
			{
				connWidthOrHeight = this.GetConnWidthOrHeight(1, conn);
				connWidthOrHeight2 = this.GetConnWidthOrHeight(0, conn);
			}
			if (ductWidthOrHeight < ductWidthOrHeight2 && connWidthOrHeight > connWidthOrHeight2)
			{
				connWidthOrHeight = this.GetConnWidthOrHeight(1, conn);
				connWidthOrHeight2 = this.GetConnWidthOrHeight(0, conn);
			}
			double num = (ductWidthOrHeight > ductWidthOrHeight2) ? ductWidthOrHeight : ductWidthOrHeight2;
			double num2 = (connWidthOrHeight > connWidthOrHeight2) ? connWidthOrHeight : connWidthOrHeight2;
			double num3;
			if (hypiping.Shape == ConnectorSharp.Round)
			{
				num3 = Math.PI * ductWidthOrHeight * ductWidthOrHeight2;
			}
			else
			{
				num3 = ductWidthOrHeight * ductWidthOrHeight2;
			}
			double num4;
			if (conn.Shape == ConnectorSharp.Round)
			{
				num4 = Math.PI * connWidthOrHeight * connWidthOrHeight2;
			}
			else
			{
				num4 = connWidthOrHeight * connWidthOrHeight2;
			}
			XYZ origin = conn.Origin;
			XYZ startXYZ = conn.Origin + conn.CoordinateSystem.BasisZ * (num + num2) * 0.85;
			Duct duct2 = YJKRevitTools.NewDuct(duct, startXYZ, conn.Origin);
			if (num3 <= num4)
			{
				this.DuctSetParameter(duct2, hypiping.StartConnector.Connector);
			}
			else
			{
				this.DuctSetParameter(conn, duct2, false);
			}
			document.Regenerate();
			this.ConnNewDuctAndConnector(conn, duct2);
			new YJKRotateFamilyInstance().MovePiping(duct2);
			Connector distanceConnector = new YJKPiping<Duct>(duct2).GetDistanceConnector(origin);
			createdDuct = duct2;
			Parameter parameter = duct.GetParameter(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM);
			createdDuct.GetParameter(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM).Set(parameter.AsElementId());
			return distanceConnector;
		}

		public double GetConnWidthOrHeight(int connShape, Connector conn)
		{
			double result = -1.0;
			if (conn.Shape == ConnectorSharp.Round)
			{
				result = conn.Radius * 2.0;
			}
			if (conn.Shape == ConnectorSharp.Rect)
			{
				if (connShape == 0)
				{
					result = conn.Width;
				}
				else
				{
					result = conn.Height;
				}
			}
			return result;
		}

		public void CheckAndChangDuctWH(double width, double height, Duct duct)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			if (hypiping.Shape == ConnectorSharp.Rect)
			{
				double num = duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM).AsDouble();
				double num2 = duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM).AsDouble();
				if (Math.Abs(width / 304.8 - num) > 1E-05)
				{
					num2 = duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM).AsDouble();
					num = duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM).AsDouble();
				}
				if (Math.Abs(hypiping.GetDuctWidthOrHeight(0) - width / 304.8) > 0.0001)
				{
					duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM).Set(num);
					duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM).Set(num2);
				}
			}
		}

		public bool DuctConnectTerminalByElbow(Duct duct, FamilyInstance airTerminal, ref string connMSG, int alignType)
		{
			bool result = false;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				double num = validConnector.CoordinateSystem.BasisZ.AngleTo(hypiping.EndConnector.Direction);
				if (Math.Abs(Math.PI*.5 - num) > 0.017453292519943295)
				{
					connMSG = "风口连接口方向必须与风管垂直！";
					return result;
				}
				XYZ[] array = new XYZ[]
				{
					validConnector.Origin,
					this.GetConnectorDirectionPoint(validConnector)
				};
				XYZ origin = hypiping.StartConnector.Origin;
				XYZ origin2 = hypiping.EndConnector.Origin;
				XYZ dlineP = YJKRevitTools.GetDLineP(origin, origin2, array[0], array[1]);
				XYZ dlineP2 = YJKRevitTools.GetDLineP(array[0], array[1], origin, origin2);
				if (dlineP == null || dlineP2 == null)
				{
					connMSG = "风口和风管没有交点！";
					return result;
				}
				if (dlineP2.IsAlmostEqualTo(dlineP, 0.0001))
				{
					Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, dlineP2, validConnector.Origin);
					this.DuctSetParameter(validConnector, duct2, false);
					document.Regenerate();
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle = hypiping.GetXAxisAngle();
						hypiping2.Rotate(Math.PI*.5 - xaxisAngle);
					}
					Connector closeConnector = hypiping2.GetCloseConnector(validConnector.Origin, false);
					if (!this.IsConnectSameDirection(validConnector, closeConnector))
					{
						hypiping2.Rotate(Math.PI*.5);
					}
					closeConnector.ConnectTo(validConnector);
					Connector closeConnector2 = hypiping.GetCloseConnector(dlineP2, false);
					Connector closeConnector3 = hypiping2.GetCloseConnector(dlineP2, false);
					if (closeConnector2 == null || closeConnector2.IsConnected)
					{
						connMSG = "风管与风口连接的接口已连接！";
						return result;
					}
					if (closeConnector3 == null)
					{
						connMSG = "风口连接口创建风管有误！";
						return result;
					}
					document.Create.NewElbowFitting(closeConnector2, closeConnector3);
				}
				else
				{
					double num2 = dlineP2.DistanceTo(dlineP);
					if (hypiping.GetWeightOrDiameter() * 2.0 > num2)
					{
						connMSG = "风口与风管距离不合适，请调节过后再进行连接操作！";
						return result;
					}
					Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, dlineP, validConnector.Origin);
					document.Regenerate();
					this.DuctSetParameter(validConnector, duct3, false);
					Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, dlineP, dlineP2);
					this.DuctSetParameter(validConnector, duct4, false);
					document.Regenerate();
					this.AlignDucts(hypiping.Piping, duct4, alignType);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct4);
					if (hypiping4.IsXAxis() && hypiping4.IsRect())
					{
						hypiping4.Rotate90();
					}
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle2 = hypiping.GetXAxisAngle();
						hypiping3.Rotate(Math.PI - xaxisAngle2);
					}
					Connector closeConnector4 = hypiping3.GetCloseConnector(validConnector.Origin, false);
					if (!this.IsConnectSameDirection(validConnector, closeConnector4))
					{
						hypiping3.Rotate(Math.PI*.5);
					}
					closeConnector4.ConnectTo(validConnector);
					this.ConnHYDuctsByElbow(hypiping3, hypiping4, dlineP, document);
					this.ConnHYDuctsByElbow(hypiping, hypiping4, dlineP2, document);
				}
				result = true;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = false;
			}
			return result;
		}

		private void ConnHYDuctsByElbow(YJKPiping<Duct> hyDuct1, YJKPiping<Duct> hyDuct2, XYZ point, Autodesk.Revit.DB.Document doc)
		{
			string empty = string.Empty;
			FamilySymbol familySymbol = doc.GetElementById(hyDuct1.Piping.DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)1, 0).MEPPartId) as FamilySymbol;
			if (familySymbol == null)
			{
				if (!this.IsExistElbowFitting(doc, out familySymbol))
				{
					SubTransaction subTransaction = null;
					try
					{
						subTransaction = new SubTransaction(doc);
						subTransaction.Start();
						familySymbol = new ConnectDuctManage().GetFamilySymbol("内外弧形矩形弯头", doc, ref empty);
						if (familySymbol == null)
						{
							throw new Exception("弯头族不存在或者加载地址不正确！");
						}
						UnifiedModified.ActivateFamilySymbol(familySymbol);
						doc.Regenerate();
						subTransaction.Commit();
					}
					catch (Exception ex)
					{
						if ((int)subTransaction.GetStatus() != 3 && (int)subTransaction.GetStatus() == 1)
						{
							subTransaction.RollBack();
						}
						throw ex;
					}
				}
				if (!hyDuct1.ChangeFirstElbow(familySymbol))
				{
					throw new Exception("设置风管弯头，\n首选连接件出错！");
				}
			}
			if (!(doc.GetElementById(hyDuct2.Piping.DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)1, 0).MEPPartId) is FamilySymbol))
			{
				FamilySymbol fs = familySymbol;
				if (!hyDuct2.ChangeFirstElbow(fs))
				{
					throw new Exception("设置风管弯头，\n首选连接件出错！");
				}
			}
			if (Math.Abs(hyDuct1.GetWeightOrDiameter() - hyDuct2.GetWeightOrDiameter()) > 0.0001 || Math.Abs(hyDuct1.GetHeightOrDiameter() - hyDuct2.GetHeightOrDiameter()) > 0.0001)
			{
				FamilySymbol familySymbol2 = doc.GetElementById(hyDuct1.Piping.DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)4, 0).MEPPartId) as FamilySymbol;
				if (familySymbol2 == null)
				{
					familySymbol2 = new ConnectDuctManage().GetFamilySymbol("矩形变径", doc, ref empty);
					if (familySymbol2 == null)
					{
						throw new Exception("变径族不存在或者加载地址不正确！");
					}
				}
				hyDuct1.ChangeFirstTransition(familySymbol2);
				hyDuct2.ChangeFirstTransition(familySymbol2);
			}
			Connector nearConnectorToPoint = hyDuct1.GetNearConnectorToPoint(point);
			Connector nearConnectorToPoint2 = hyDuct2.GetNearConnectorToPoint(point);
			doc.Create.NewElbowFitting(nearConnectorToPoint, nearConnectorToPoint2);
			doc.Regenerate();
		}

		private bool IsExistElbowFitting(Autodesk.Revit.DB.Document doc, out FamilySymbol fSymbol)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_DuctFitting);
			fSymbol = null;
			bool result = false;
			foreach (Element element in filteredElementCollector)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (familySymbol != null && familySymbol.Family.Name.Contains("弯头"))
				{
					fSymbol = familySymbol;
					result = true;
					break;
				}
			}
			return result;
		}

		public bool DuctConnectTerminalByElbow(Duct duct, FamilyInstance airTerminal, ref string connMSG)
		{
			bool result = false;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				double num = validConnector.CoordinateSystem.BasisZ.AngleTo(hypiping.EndConnector.Direction);
				if (Math.Abs(Math.PI*.5 - num) > 0.017453292519943295)
				{
					connMSG = "风口连接口方向必须与风管垂直！";
					return result;
				}
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				XYZ origin = hypiping.StartConnector.Origin;
				XYZ origin2 = hypiping.EndConnector.Origin;
				XYZ dlineP = YJKRevitTools.GetDLineP(list[0], list[1], origin, origin2);
				XYZ dlineP2 = YJKRevitTools.GetDLineP(origin, origin2, list[0], list[1]);
				if (dlineP == null || dlineP2 == null)
				{
					connMSG = "风口和风管没有交点！";
					return result;
				}
				if (dlineP.DistanceTo(dlineP2) > 0.00984251968503937)
				{
					connMSG = "风口方向绘制风管\n与原风管没有交点，\n不能进行连接！";
					return result;
				}
				dlineP2.DistanceTo(validConnector.Origin);
				XYZ xyz = dlineP2.Subtract(list[0]).Normalize();
				if (!validConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz, 0.001))
				{
					connMSG = "风口连接口方向绘制风管\n不能与原风管进行连接！";
					return result;
				}
				Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, dlineP, validConnector.Origin);
				this.DuctSetParameter(validConnector, duct2, false);
				document.Regenerate();
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
				if (!hypiping.IsYAxis())
				{
					double xaxisAngle = hypiping.GetXAxisAngle();
					hypiping2.Rotate(Math.PI*.5 - xaxisAngle);
				}
				Connector closeConnector = hypiping.GetCloseConnector(dlineP, false);
				Connector closeConnector2 = hypiping2.GetCloseConnector(dlineP, false);
				if (closeConnector == null || closeConnector.IsConnected)
				{
					connMSG = "风管与风口连接的接口已连接！";
					return result;
				}
				if (closeConnector2 == null)
				{
					connMSG = "风口连接口创建风管有误！";
					return result;
				}
				document.Create.NewElbowFitting(closeConnector, closeConnector2);
				result = true;
			}
			catch (Exception)
			{
				result = false;
				return result;
			}
			return result;
		}

		public bool ConnectAirTerminalByElbow(Duct duct, FamilyInstance airTerminal, ref string connMSG)
		{
			bool result = false;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				XYZ curveIntersection = this.GetCurveIntersection(curve2, curve, 0.01);
				if (validConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.0001) || validConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0), 0.0001))
				{
					if (curveIntersection == null)
					{
						connMSG = "风口位置没有与风管对齐，\n无法完成连接操作！";
						return result;
					}
					if (validConnector.IsConnected)
					{
						connMSG = "风口已连接！";
						return result;
					}
					if (!curveIntersection.Subtract(validConnector.Origin).Normalize().IsAlmostEqualTo(validConnector.CoordinateSystem.BasisZ, 0.001))
					{
						connMSG = "风口连接口方向不正确，无法进行连接！";
						return result;
					}
					Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
					this.DuctSetParameter(validConnector, duct2, false);
					document.Regenerate();
					this.ConnNewDuctAndConnector(validConnector, duct2);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					Connector closeConnector = hypiping.GetCloseConnector(curveIntersection, false);
					Connector closeConnector2 = hypiping2.GetCloseConnector(curveIntersection, false);
					if (closeConnector == null || closeConnector.IsConnected)
					{
						connMSG = "风管与风口连接的接口已连接！";
						return result;
					}
					if (closeConnector2 == null)
					{
						connMSG = "风口连接口创建风管有误！";
						return result;
					}
					document.Create.NewElbowFitting(closeConnector, closeConnector2);
					result = true;
				}
				else
				{
					double offSet = hypiping.GetOffSet();
					double z = validConnector.Origin.Z;
					if (Math.Abs(offSet - z) > 0.00984251968503937)
					{
						connMSG = "风口位置不正确，请调风口与风管同样标高后再进行连接！";
						return result;
					}
					YJKLine hyline = new YJKLine(list[0], list[1]);
					YJKLine hyline2 = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
					if (hyline.IsLineOnLine(hyline2.Line))
					{
						if (Math.Abs(offSet - z) != 0.0)
						{
							connMSG = "风口位置不正确，请调风口与风管同样标高后再进行连接！";
							return result;
						}
						Connector closeConnector3 = hypiping.GetCloseConnector(validConnector.Origin, false);
						if (!validConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(closeConnector3.CoordinateSystem.BasisZ * -1.0, 0.001))
						{
							connMSG = "风口连接口方向不正确，无法进行连接！";
							return result;
						}
						Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, validConnector.Origin, closeConnector3.Origin);
						this.DuctSetParameter(validConnector, duct3, false);
						result = true;
						return result;
					}
					else
					{
						if (!curveIntersection.Subtract(validConnector.Origin).Normalize().IsAlmostEqualTo(validConnector.CoordinateSystem.BasisZ, 0.001))
						{
							connMSG = "风口连接口方向不正确，无法进行连接！";
							return result;
						}
						Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
						this.DuctSetParameter(validConnector, duct4, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector, duct4);
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct4);
						Connector closeConnector4 = hypiping.GetCloseConnector(curveIntersection, false);
						Connector closeConnector5 = hypiping3.GetCloseConnector(curveIntersection, false);
						if (closeConnector4 == null || closeConnector4.IsConnected)
						{
							connMSG = "风管与风口连接的接口已连接！";
							return result;
						}
						if (closeConnector5 == null)
						{
							connMSG = "风口连接口创建风管有误！";
							return result;
						}
						document.Create.NewElbowFitting(closeConnector4, closeConnector5);
						result = true;
					}
				}
			}
			catch (Exception)
			{
				result = false;
				return result;
			}
			return result;
		}

		private Duct FlexDuctConnection(Autodesk.Revit.DB.Document doc, XYZ intersection, Connector connector, YJKPiping<Duct> myDuct)
		{
			SubTransaction subTransaction = null;
			Duct result;
			try
			{
				subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				double num = UnitConvert.CovertToAPI(100.0, (Autodesk.Revit.DB.DisplayUnitType)2);
				XYZ endXYZ = connector.Origin + connector.CoordinateSystem.BasisZ * num;
				FlexDuctType flexDuctType = YJKRevitTools.GetContainNameElement(YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_FlexDuctCurves), YJKRevitTools.GetConnectorSharpName(connector)) as FlexDuctType;
				Duct duct = YJKRevitTools.NewDuct(myDuct.Piping, intersection, endXYZ);
				this.DuctSetParameter(connector, duct, false);
				hyrotateFamilyInstance.MovePiping(duct);
				Connector closeConnector = new YJKPiping<Duct>(duct).GetCloseConnector(connector.Origin, false);
				FlexDuct flexDuct = doc.Create.NewFlexDuct(connector, closeConnector, flexDuctType);
				this.SetFlexDuctByDuct(connector, flexDuct);
				doc.Regenerate();
				subTransaction.Commit();
				result = duct;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private void ConnTerminalAndDuctByFlexDuct(FlexDuct flexDuct, Connector ductConn)
		{
			ConnectorManager connectorManager = flexDuct.ConnectorManager;
			if (connectorManager == null)
			{
				return;
			}
			ConnectorSetIterator connectorSetIterator = connectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (connector != null && connector.Origin.IsAlmostEqualTo(ductConn.Origin, 1E-05))
				{
					connector.ConnectTo(ductConn);
				}
			}
		}

		private void DuctSetParameter(Duct duct)
		{
			Parameter parameter = duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
			Parameter parameter2 = duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
			parameter.Set(HYDuctConnect.mmToFeet(50.0));
			parameter2.Set(HYDuctConnect.mmToFeet(50.0));
		}

		private bool IsConnectSameDirection(Connector connector1, Connector connector2)
		{
			double num = connector1.CoordinateSystem.BasisY.AngleTo(connector2.CoordinateSystem.BasisY);
			return Math.Abs(num - 0.0) < 0.001 || Math.Abs(num - Math.PI) < 0.001;
		}

		private void DuctSetParameter(Connector connector, Duct duct, bool isNozzle = false)
		{
			if (connector.Shape == ConnectorSharp.Rect)
			{
				double num = connector.Height;
				double num2 = connector.Width;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				if (Math.Abs(hypiping.StartConnector.Origin.Z - hypiping.EndConnector.Origin.Z) < 0.01)
				{
                    if (duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM).AsDouble() > duct.GetParameter((BuiltInParameter)(-1114102)).AsDouble())
					{
						if (num2 < num)
						{
							num2 = connector.Height;
							num = connector.Width;
						}
					}
					else if (num2 > num)
					{
						num2 = connector.Height;
						num = connector.Width;
					}
				}
				ElementId elementId;
				if (connector.Owner.GetYJKLevel() != null)
				{
					elementId = connector.Owner.GetYJKLevel().Id;
				}
				else
				{
					elementId = connector.Owner.GetParameter(BuiltInParameter.INSTANCE_SCHEDULE_ONLY_LEVEL_PARAM).AsElementId();
				}
				if (duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM) != null)
				{
					Parameter parameter = duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
					Parameter parameter2 = duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
					if (isNozzle)
					{
						parameter.Set(num);
						parameter2.Set(num2);
					}
					else
					{
						parameter.Set(num);
						parameter2.Set(num2);
					}
				}
				else
				{
					duct.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM).Set(num);
				}
				duct.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).Set(elementId);
				return;
			}
			if (connector.Shape == ConnectorSharp.Round)
			{
				double radius = connector.Radius;
				ElementId elementId2;
				if (connector.Owner.GetYJKLevel() != null)
				{
					elementId2 = connector.Owner.GetYJKLevel().Id;
				}
				else
				{
					elementId2 = connector.Owner.GetParameter(BuiltInParameter.INSTANCE_SCHEDULE_ONLY_LEVEL_PARAM).AsElementId();
				}
				if (duct.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM) != null)
				{
					duct.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM).Set(radius * 2.0);
				}
				else
				{
					duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM).Set(radius * 2.0);
					duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM).Set(radius * 2.0);
				}
				duct.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).Set(elementId2);
			}
		}

		private void DuctSetParameter(Duct duct, Connector connector)
		{
			if (connector.Shape == ConnectorSharp.Rect)
			{
				double height = connector.Height;
				double width = connector.Width;
				duct.GetParameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM).Set(height);
				duct.GetParameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM).Set(width);
				return;
			}
			if (connector.Shape == ConnectorSharp.Round)
			{
				double radius = connector.Radius;
				duct.GetParameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM).Set(radius * 2.0);
			}
		}

		private static double mmToFeet(double val)
		{
			return val / 304.8;
		}

		private bool IsConnectorCloseDuct(Duct duct, Connector conn)
		{
			bool flag = false;
			XYZ origin = conn.Origin;
			XYZ basisZ = conn.CoordinateSystem.BasisZ;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			XYZ xyz = (hypiping.StartConnector.Origin - hypiping.EndConnector.Origin).Normalize();
			XYZ project = hypiping.GetProject(origin);
			if (Math.Abs(basisZ.DotProduct(xyz)) > 0.0001)
			{
				throw new Exception("风口接口与风管的位置不垂直，不能进行连接");
			}
			if (hypiping.Shape == ConnectorSharp.Rect)
			{
				double num = hypiping.GetLocationCurve().Distance(origin);
				double height = hypiping.Piping.Height;
				double width = hypiping.Piping.Width;
				if (num < (height + width) / 2.0)
				{
					XYZ xyz2 = null;
					if (basisZ.IsAlmostEqualTo(XYZ.BasisZ))
					{
						if (Math.Abs(num - height / 2.0) < 0.001 && origin.Z < hypiping.StartConnector.Origin.Z)
						{
							flag = true;
						}
						else
						{
							xyz2 = project - height / 2.0 * XYZ.BasisZ;
						}
					}
					else if (basisZ.IsAlmostEqualTo(-XYZ.BasisZ))
					{
						if (Math.Abs(num - height / 2.0) < 0.001 && origin.Z > hypiping.StartConnector.Origin.Z)
						{
							flag = true;
						}
						else
						{
							xyz2 = project + height / 2.0 * XYZ.BasisZ;
						}
					}
					else if (basisZ.DotProduct(xyz) < 0.0001)
					{
						if (Math.Abs(num - width / 2.0) < 0.001)
						{
							flag = true;
						}
						else
						{
							XYZ xyz3 = (new XYZ(origin.X, origin.Y, 0.0) - new XYZ(project.X, project.Y, 0.0)).Normalize();
							xyz2 = project + width / 2.0 * xyz3;
						}
					}
					if (!flag && xyz2 != null)
					{
						Location location = conn.Owner.Location;
						XYZ xyz4 = xyz2 - origin;
						location.Move(xyz4);
						flag = true;
					}
				}
			}
			return flag;
		}

		public bool DuctConnectAirTerminalTakeOff(Duct duct, FamilyInstance airTerminal)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				if (validConnector != null && validConnector.IsConnected)
				{
					throw new Exception("风口已经有风管连接");
				}
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				bool flag2;
				if (this.IsConnectorCloseDuct(duct, validConnector))
				{
					bool flag = false;
					List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(document, typeof(FamilySymbol), BuiltInCategory.OST_DuctFitting);
					FamilySymbol familySymbol = null;
					foreach (Element element in elementByCategory)
					{
						if ((element as FamilySymbol).Family.Name == "插接头")
						{
							flag = true;
							familySymbol = (element as FamilySymbol);
							break;
						}
					}
					if (!flag)
					{
						Family family = null;
						if (document.LoadFamily(UnifiedModified.GetTakeOffFittingPath() + "插接头.rfa", out family))
						{
							foreach (FamilySymbol familySymbol2 in family.GetSymbols())
							{
								if (familySymbol2 != null)
								{
									familySymbol = familySymbol2;
									break;
								}
							}
						}
					}
					if (!hypiping.CheckFirstTakeOff("插接头"))
					{
						hypiping.ChangeFirstTakeOff(familySymbol);
					}
					XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(validConnector, -1);
					Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, connectorDirectionPoint, validConnector.Origin);
					this.DuctSetParameter(validConnector, duct2, false);
					document.Regenerate();
					this.ConnNewDuctAndConnector(validConnector, duct2);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					FamilyInstance familyInstance = document.Create.NewTakeoffFitting(hypiping2.GetAppropriateConnector(validConnector.Origin), hypiping.Piping);
					document.DeleteElement(duct2);
					YJKRevitTools.GetElementUnConnector(familyInstance).ConnectTo(validConnector);
					familyInstance.Symbol = familySymbol;
					flag2 = true;
				}
				else if (curveIntersection != null)
				{
					Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
					this.DuctSetParameter(validConnector, duct3, false);
					document.Regenerate();
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle = hypiping.GetXAxisAngle();
						hypiping3.Rotate(Math.PI - xaxisAngle);
					}
					Connector closeConnector = hypiping3.GetCloseConnector(validConnector.Origin, false);
					if (!this.IsConnectSameDirection(validConnector, closeConnector))
					{
						hypiping3.Rotate(Math.PI*.5);
					}
					closeConnector.ConnectTo(validConnector);
					document.Create.NewTakeoffFitting(hypiping3.GetAppropriateConnector(curveIntersection), hypiping.Piping);
					flag2 = true;
				}
				else
				{
					int num = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
					if (num != 1 || ductAirTerminalPoint == null || ductAirTerminalPoint.Count <= 1)
					{
						throw new Exception("无法进行侧连接，可能原因是风管长度不够");
					}
					Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
					this.DuctSetParameter(validConnector, duct4, false);
					Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
					this.DuctSetParameter(validConnector, duct5, false);
					document.Regenerate();
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct4);
					YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct5);
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle2 = hypiping.GetXAxisAngle();
						hypiping4.Rotate(Math.PI - xaxisAngle2);
					}
					if (hypiping5.IsXAxis() && hypiping5.IsRect())
					{
						hypiping5.Rotate90();
					}
					Connector closeConnector2 = hypiping4.GetCloseConnector(validConnector.Origin, false);
					if (!this.IsConnectSameDirection(validConnector, closeConnector2))
					{
						hypiping4.Rotate(Math.PI*.5);
					}
					closeConnector2.ConnectTo(validConnector);
					document.Create.NewElbowFitting(hypiping4.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]));
					document.Create.NewTakeoffFitting(hypiping5.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping.Piping);
					flag2 = true;
				}
				subTransaction.Commit();
				result = flag2;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctConnectAirTerminalTakeOff(Duct duct, FamilyInstance airTerminal, ref string connMSG, int alignType)
		{
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				if (validConnector != null && validConnector.IsConnected)
				{
					connMSG = "风口已经有风管连接";
					result = false;
				}
				else
				{
					IList<XYZ> list = new List<XYZ>();
					list.Add(validConnector.Origin);
					list.Add(this.GetConnectorDirectionPoint(validConnector));
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
					Parameter parameter = duct.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM);
					Parameter parameter2 = airTerminal.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM);
					if (parameter != null && parameter2 != null && parameter.AsDouble() != parameter2.AsDouble())
					{
						XYZ dlineP = YJKRevitTools.GetDLineP(list[0], list[1], hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
						if (YJKRevitTools.GetDLineP(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, list[0], list[1]).DistanceTo(dlineP) < hypiping.GetDiameter())
						{
							connMSG = "风口标高与风管标高相差太小，\n无法构成侧连接！";
							return false;
						}
					}
					Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
					Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
					curve.MakeUnbound();
					curve2.MakeUnbound();
					XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
					bool flag2;
					if (this.IsConnectorCloseDuct(duct, validConnector))
					{
						bool flag = false;
						List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(document, typeof(FamilySymbol), BuiltInCategory.OST_DuctFitting);
						FamilySymbol familySymbol = null;
						foreach (Element element in elementByCategory)
						{
							if ((element as FamilySymbol).Family.Name == "插接头")
							{
								flag = true;
								familySymbol = (element as FamilySymbol);
								break;
							}
						}
						if (!flag)
						{
							Family family = null;
							if (document.LoadFamily(UnifiedModified.GetTakeOffFittingPath() + "插接头.rfa", out family))
							{
								foreach (FamilySymbol familySymbol2 in family.GetSymbols())
								{
									if (familySymbol2 != null)
									{
										familySymbol = familySymbol2;
										break;
									}
								}
							}
						}
						if (!hypiping.CheckFirstTakeOff("插接头"))
						{
							hypiping.ChangeFirstTakeOff(familySymbol);
						}
						XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(validConnector, -1);
						Duct duct2 = this.CreatDuctAndConnTerminal(connectorDirectionPoint, validConnector, hypiping);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
						FamilyInstance familyInstance = document.Create.NewTakeoffFitting(hypiping2.GetAppropriateConnector(validConnector.Origin), hypiping.Piping);
						document.DeleteElement(duct2);
						YJKRevitTools.GetElementUnConnector(familyInstance).ConnectTo(validConnector);
						familyInstance.Symbol = familySymbol;
						document.Regenerate();
						this.AlignDucts(hypiping.Piping, duct2, alignType);
						flag2 = true;
					}
					else if (curveIntersection != null)
					{
						Duct duct3 = this.CreatDuctAndConnTerminal(curveIntersection, validConnector, hypiping);
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
						if (!hypiping.IsYAxis())
						{
							hypiping.GetXAxisAngle();
						}
						document.Create.NewTakeoffFitting(hypiping3.GetAppropriateConnector(curveIntersection), hypiping.Piping);
						document.Regenerate();
						this.AlignDucts(hypiping.Piping, duct3, alignType);
						flag2 = true;
					}
					else
					{
						int num = 1;
						IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
						if (num != 1 || ductAirTerminalPoint == null || ductAirTerminalPoint.Count <= 1)
						{
							connMSG = "无法进行侧连接，可能原因是风管长度不够";
							return false;
						}
						Duct duct4 = this.CreatDuctAndConnTerminal(ductAirTerminalPoint[1], validConnector, hypiping);
						this.DuctSetParameter(validConnector, duct4, false);
						Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct5, false);
						document.Regenerate();
						YJKPiping<Duct> hyDuct = new YJKPiping<Duct>(duct4);
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct5);
						if (!hypiping.IsYAxis())
						{
							hypiping.GetXAxisAngle();
						}
						if (hypiping4.IsXAxis() && hypiping4.IsRect())
						{
							hypiping4.Rotate90();
						}
						this.ConnElbowDucts(hyDuct, hypiping4, ductAirTerminalPoint[1], document);
						document.Create.NewTakeoffFitting(hypiping4.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping.Piping);
						document.Regenerate();
						this.AlignDucts(hypiping.Piping, duct5, alignType);
						flag2 = true;
					}
					result = flag2;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool DuctConnectAirTerminalTakeOff(Duct duct, FamilyInstance airTerminal, ref string connMSG)
		{
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				if (validConnector != null && validConnector.IsConnected)
				{
					connMSG = "风口已经有风管连接";
					result = false;
				}
				else
				{
					IList<XYZ> list = new List<XYZ>();
					list.Add(validConnector.Origin);
					list.Add(this.GetConnectorDirectionPoint(validConnector));
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
					if (duct.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() != airTerminal.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble())
					{
						XYZ dlineP = YJKRevitTools.GetDLineP(list[0], list[1], hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
						if (YJKRevitTools.GetDLineP(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, list[0], list[1]).DistanceTo(dlineP) < hypiping.GetDiameter())
						{
							connMSG = "风口标高与风管标高相差太小，\n无法构成侧连接！";
							return false;
						}
					}
					Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
					Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
					curve.MakeUnbound();
					curve2.MakeUnbound();
					XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
					bool flag2;
					if (this.IsConnectorCloseDuct(duct, validConnector))
					{
						bool flag = false;
						List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(document, typeof(FamilySymbol), BuiltInCategory.OST_DuctFitting);
						FamilySymbol familySymbol = null;
						foreach (Element element in elementByCategory)
						{
							if ((element as FamilySymbol).Family.Name == "插接头")
							{
								flag = true;
								familySymbol = (element as FamilySymbol);
								break;
							}
						}
						if (!flag)
						{
							Family family = null;
							if (document.LoadFamily(UnifiedModified.GetTakeOffFittingPath() + "插接头.rfa", out family))
							{
								foreach (FamilySymbol familySymbol2 in family.GetSymbols())
								{
									if (familySymbol2 != null)
									{
										familySymbol = familySymbol2;
										break;
									}
								}
							}
						}
						if (!hypiping.CheckFirstTakeOff("插接头"))
						{
							hypiping.ChangeFirstTakeOff(familySymbol);
						}
						XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(validConnector, -1);
						Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, connectorDirectionPoint, validConnector.Origin);
						this.DuctSetParameter(validConnector, duct2, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector, duct2);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
						FamilyInstance familyInstance = document.Create.NewTakeoffFitting(hypiping2.GetAppropriateConnector(validConnector.Origin), hypiping.Piping);
						document.DeleteElement(duct2);
						YJKRevitTools.GetElementUnConnector(familyInstance).ConnectTo(validConnector);
						familyInstance.Symbol = familySymbol;
						flag2 = true;
					}
					else if (curveIntersection != null)
					{
						Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
						this.DuctSetParameter(validConnector, duct3, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector, duct3);
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
						if (!hypiping.IsYAxis())
						{
							double xaxisAngle = hypiping.GetXAxisAngle();
							hypiping3.Rotate(Math.PI*.5 - xaxisAngle);
						}
						document.Create.NewTakeoffFitting(hypiping3.GetAppropriateConnector(curveIntersection), hypiping.Piping);
						flag2 = true;
					}
					else
					{
						int num = 1;
						IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
						if (num != 1 || ductAirTerminalPoint == null || ductAirTerminalPoint.Count <= 1)
						{
							connMSG = "无法进行侧连接，可能原因是风管长度不够";
							return false;
						}
						Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
						this.DuctSetParameter(validConnector, duct4, false);
						Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct5, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector, duct4);
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct4);
						YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct5);
						if (!hypiping.IsYAxis())
						{
							double xaxisAngle2 = hypiping.GetXAxisAngle();
							hypiping4.Rotate(Math.PI*.5 - xaxisAngle2);
						}
						if (hypiping5.IsXAxis() && hypiping5.IsRect())
						{
							hypiping5.Rotate90();
						}
						document.Create.NewElbowFitting(hypiping4.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]));
						document.Create.NewTakeoffFitting(hypiping5.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping.Piping);
						flag2 = true;
					}
					result = flag2;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool DuctConnSideTerminalTakeOff(Duct duct, FamilyInstance sideTerminal, ref string connMSG, int connType, bool isNozzle = false)
		{
			bool result = false;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, sideTerminal);
				if (elementConnector != null && elementConnector.IsConnected)
				{
					connMSG = "风口已经有风管连接";
					return false;
				}
				YJKPiping<Duct> hyDuct = new YJKPiping<Duct>(duct);
				if (connType != 11)
				{
					if (connType == 22 || connType == 33)
					{
						this.ConnSideFaceTerminal(sideTerminal, hyDuct, connType, isNozzle);
						result = true;
					}
				}
				else
				{
					this.ConnEndFaceTerminal(sideTerminal, hyDuct);
					result = true;
				}
			}
			catch (Exception ex)
			{
				result = false;
				throw ex;
			}
			return result;
		}

		public bool DuctConnSideTerminalTakeOff(Duct duct, FamilyInstance sideTerminal, ref string connMSG, int connType, int nozzleType)
		{
			bool result = false;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, sideTerminal);
				if (elementConnector != null && elementConnector.IsConnected)
				{
					connMSG = "喷口已经有风管连接";
					return false;
				}
				YJKPiping<Duct> hyDuct = new YJKPiping<Duct>(duct);
				if (connType != 11)
				{
					if (connType == 22 || connType == 33)
					{
						this.ConnSideFaceTerminal(sideTerminal, hyDuct, nozzleType);
						result = true;
					}
				}
				else
				{
					this.ConnEndFaceTerminal(sideTerminal, hyDuct);
					result = true;
				}
			}
			catch (Exception ex)
			{
				result = false;
				throw ex;
			}
			return result;
		}

		private void ConnEndFaceTerminal(FamilyInstance sideTerminal, YJKPiping<Duct> hyDuct)
		{
			Autodesk.Revit.DB.Document document = hyDuct.Piping.Document;
			Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, sideTerminal);
			Connector closeConnector = hyDuct.GetCloseConnector(elementConnector.Origin, false);
			bool flag = this.BeEqualEqTerminalAndDuctSize(closeConnector, elementConnector);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			if (flag)
			{
				XYZ moveVector = closeConnector.Origin.Subtract(elementConnector.Origin);
				hyrotateFamilyInstance.MoveElem(moveVector, sideTerminal);
				closeConnector.ConnectTo(elementConnector);
				return;
			}
			YJKLine hyline = new YJKLine(closeConnector.Origin, closeConnector.Origin.Add(closeConnector.CoordinateSystem.BasisZ));
			hyline.MakeUnBound();
			XYZ moveVector2 = hyline.GetProject(elementConnector.Origin).Subtract(elementConnector.Origin);
			double z = hyDuct.StartConnector.Origin.Z;
			hyrotateFamilyInstance.MoveElem(moveVector2, sideTerminal);
			XYZ xyz = elementConnector.Origin;
			xyz = new XYZ(xyz.X, xyz.Y, z);
			XYZ xyz2 = this.GetConnectorDirectionPoint(elementConnector, 1.5);
			xyz2 = new XYZ(xyz2.X, xyz2.Y, z);
			Duct duct = YJKRevitTools.NewDuct(hyDuct.Piping, xyz, xyz2);
			this.DuctSetParameter(elementConnector, duct, false);
			document.Regenerate();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			this.NewTransitionFitting(new List<Duct>
			{
				hyDuct.Piping,
				duct
			});
			document.Regenerate();
			Connector connectConnector = YJKRevitTools.GetConnectConnector(hypiping.StartConnector.Connector.IsConnected ? hypiping.StartConnector.Connector : hypiping.EndConnector.Connector);
			document.DeleteElement(duct);
			XYZ moveVector3 = connectConnector.Origin.Subtract(elementConnector.Origin);
			hyrotateFamilyInstance.MoveElem(moveVector3, sideTerminal);
			connectConnector.ConnectTo(elementConnector);
		}

		private void ConnSideFaceTerminal(FamilyInstance sideTerminal, YJKPiping<Duct> hyDuct, int connType, bool isNozzle = false)
		{
			Autodesk.Revit.DB.Document document = hyDuct.Piping.Document;
			Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, sideTerminal);
			XYZ origin = elementConnector.Origin;
			XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(elementConnector, 0.16404199475065617);
			Duct duct = YJKRevitTools.NewDuct(hyDuct.Piping, origin, connectorDirectionPoint);
			document.Regenerate();
			HYDuctConnect.RotateTerminal(sideTerminal, hyDuct, duct, document);
			if (isNozzle)
			{
				this.DuctSetParameter(duct);
			}
			else
			{
				this.DuctSetParameter(elementConnector, duct, false);
			}
			document.Regenerate();
			IList<RoutingPreferenceRule> segmentRuleFitting = UnifiedModified.GetSegmentRuleFitting(hyDuct.Piping.DuctType, 6);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			FamilySymbol familySymbol = this.ChangeFirstTakeOff(hyDuct, document);
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			this.ChangeFirstTakeOff(hypiping, document);
			hypiping.GetCloseConnector(connectorDirectionPoint, false);
			this.MoveNewDuct(hyDuct, duct);
			FamilyInstance familyInstance = this.NewTakeOffFitting(new List<Duct>
			{
				hyDuct.Piping,
				hypiping.Piping
			}, new List<XYZ>
			{
				elementConnector.Origin,
				connectorDirectionPoint
			}, familySymbol);
			if (connType == 33 && (familyInstance.HandOrientation.Normalize().IsAlmostEqualTo(XYZ.BasisZ) || familyInstance.HandOrientation.Normalize().IsAlmostEqualTo(-XYZ.BasisZ)))
			{
				YJKRotateFamilyInstance hyrotateFamilyInstance2 = new YJKRotateFamilyInstance();
				double num = document.ActiveView.RightDirection.Normalize().AngleTo(sideTerminal.HandOrientation.Normalize());
				num = ((sideTerminal.HandOrientation.Normalize().AngleTo(document.ActiveView.UpDirection) > Math.PI*.5) ? (Math.PI - num) : num);
				hyrotateFamilyInstance2.RotateElem(document, familyInstance, num * 180.0 / Math.PI);
			}
			document.DeleteElement(duct);
			Connector elementUnConnector = YJKRevitTools.GetElementUnConnector(familyInstance);
			XYZ moveVector = elementUnConnector.Origin.Subtract(elementConnector.Origin);
			hyrotateFamilyInstance.MoveElem(moveVector, sideTerminal);
			elementUnConnector.ConnectTo(elementConnector);
			familyInstance.Symbol = familySymbol;
			UnifiedModified.SetSegmentRuleFitting(hyDuct.Piping.DuctType, 6, segmentRuleFitting);
		}

		private void MoveNewDuct(YJKPiping<Duct> hyDuct, Duct newDuct)
		{
			YJKLine hyline = new YJKLine(hyDuct.StartConnector.Origin, hyDuct.EndConnector.Origin);
			hyline.MakeUnBound();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(newDuct);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ xyz = hypiping.StartConnector.Origin.Subtract(hypiping.EndConnector.Origin).Normalize();
			XYZ project = hyline.GetProject(origin);
			YJKLine hyline2 = new YJKLine(project, project.Add(xyz));
			hyline2.MakeUnBound();
			XYZ xyz2 = hyline2.GetProject(origin).Subtract(origin);
			if (xyz2 == null || xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0), 0.0001))
			{
				return;
			}
			new YJKRotateFamilyInstance().MoveElement(xyz2, newDuct);
		}

		private static void RotateTerminal(FamilyInstance sideTerminal, YJKPiping<Duct> hyDuct, Duct newDuct, Autodesk.Revit.DB.Document doc)
		{
			if (sideTerminal.HandOrientation.Normalize().IsAlmostEqualTo(XYZ.BasisZ) || sideTerminal.HandOrientation.Normalize().IsAlmostEqualTo(-XYZ.BasisZ))
			{
				LocationCurve locationCurve = hyDuct.Piping.Location as LocationCurve;
				if (locationCurve != null && locationCurve.Curve as Line != null)
				{
					XYZ direction = (locationCurve.Curve as Line).Direction;
					YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
					double num = doc.ActiveView.RightDirection.Normalize().AngleTo(direction);
					num = ((direction.AngleTo(doc.ActiveView.UpDirection) > Math.PI*.5) ? (Math.PI - num) : num);
					hyrotateFamilyInstance.RotateElem(doc, sideTerminal, num * 180.0 / Math.PI);
					List<ElementId> list = new List<ElementId>
					{
						newDuct.Id
					};
					Line line = Line.CreateBound((sideTerminal.Location as LocationPoint).Point, (sideTerminal.Location as LocationPoint).Point + XYZ.BasisZ);
					if (line != null)
					{
						ElementTransformUtils.RotateElements(doc, list, line, Math.PI*.5 + num);
					}
					doc.Regenerate();
				}
			}
		}

		private void ConnSideFaceTerminal(FamilyInstance sideTerminal, YJKPiping<Duct> hyDuct, int nozzleType)
		{
			Autodesk.Revit.DB.Document document = hyDuct.Piping.Document;
			Connector elementConnector = YJKRevitTools.GetElementConnector((Domain)1, sideTerminal);
			XYZ origin = elementConnector.Origin;
			XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(elementConnector, 1.5);
			Duct duct = YJKRevitTools.NewDuct(hyDuct.Piping, origin, connectorDirectionPoint);
			this.DuctSetParameter(elementConnector, duct, false);
			document.Regenerate();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			FamilySymbol familySymbol = this.ChangeFirstTakeOff(hyDuct, document, nozzleType);
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			this.ChangeFirstTakeOff(hypiping, document, nozzleType);
			hypiping.GetCloseConnector(connectorDirectionPoint, false);
			FamilyInstance familyInstance = this.NewTakeOffFitting(new List<Duct>
			{
				hyDuct.Piping,
				hypiping.Piping
			}, new List<XYZ>
			{
				elementConnector.Origin,
				connectorDirectionPoint
			}, familySymbol);
			document.DeleteElement(duct);
			Connector elementUnConnector = YJKRevitTools.GetElementUnConnector(familyInstance);
			XYZ moveVector = elementUnConnector.Origin.Subtract(elementConnector.Origin);
			hyrotateFamilyInstance.MoveElem(moveVector, sideTerminal);
			elementUnConnector.ConnectTo(elementConnector);
			familyInstance.Symbol = familySymbol;
		}

		private bool BeEqualEqTerminalAndDuctSize(Connector ductConn, Connector fiConn)
		{
			bool result = false;
			double num = 0.001;
			if (ductConn.Shape != fiConn.Shape)
			{
				return false;
			}
			ConnectorProfileType shape = ductConn.Shape;
			if (shape != null)
			{
                if ((int)shape != 1)
				{
					result = false;
				}
				else if (Math.Abs(ductConn.Width - fiConn.Width) < num && Math.Abs(ductConn.Height - fiConn.Height) < num)
				{
					result = true;
				}
			}
			else if (Math.Abs(ductConn.Radius - fiConn.Radius) < num)
			{
				result = true;
			}
			return result;
		}

		private FamilySymbol ChangeFirstTakeOff(YJKPiping<Duct> hyDuct, Autodesk.Revit.DB.Document doc)
		{
			bool flag = false;
			List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(doc, typeof(FamilySymbol), BuiltInCategory.OST_DuctFitting);
			FamilySymbol familySymbol = null;
			foreach (Element element in elementByCategory)
			{
				if ((element as FamilySymbol).Family.Name == "插接头")
				{
					flag = true;
					familySymbol = (element as FamilySymbol);
					break;
				}
			}
			if (!flag)
			{
				Family family = null;
				string text = Path.Combine(Product.FamilyLocation, RevitVersion.Version, "Fitting", "插接头.rfa");
				if (doc.LoadFamily(text, out family))
				{
					foreach (FamilySymbol familySymbol2 in family.GetSymbols())
					{
						if (familySymbol2 != null)
						{
							familySymbol = familySymbol2;
							break;
						}
					}
				}
			}
			if (!hyDuct.CheckFirstTakeOff("插接头"))
			{
				hyDuct.ChangeFirstTakeOff(familySymbol);
			}
			return familySymbol;
		}

		private FamilySymbol ChangeFirstTakeOff(YJKPiping<Duct> hyDuct, Autodesk.Revit.DB.Document doc, int nozzleType)
		{
			bool flag = false;
			List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(doc, typeof(FamilySymbol), BuiltInCategory.OST_DuctFitting);
			FamilySymbol familySymbol = null;
			string text = (nozzleType == 0) ? "插接头" : "圆形活接头";
			foreach (Element element in elementByCategory)
			{
				FamilySymbol familySymbol2 = element as FamilySymbol;
				if (familySymbol2 != null && familySymbol2.Family.Name == text)
				{
					flag = true;
					familySymbol = (element as FamilySymbol);
					break;
				}
			}
			if (!flag)
			{
				Family family = null;
				string path = text + ".rfa";
				string text2 = Path.Combine(Product.FamilyLocation, RevitVersion.Version, "Fitting", path);
				if (doc.LoadFamily(text2, out family))
				{
					foreach (FamilySymbol familySymbol3 in family.GetSymbols())
					{
						if (familySymbol3 != null)
						{
							familySymbol = familySymbol3;
							break;
						}
					}
				}
			}
			if (!hyDuct.CheckFirstTakeOff(text))
			{
				hyDuct.ChangeFirstTakeOff(familySymbol);
			}
			return familySymbol;
		}

		public Duct DuctConnectAirTerminalTakeOff(Duct duct, FamilyInstance airTerminal, bool connectBatch, int terminalConnType)
		{
			Duct duct2 = null;
			SubTransaction subTransaction = null;
			Duct result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				if (curveIntersection != null)
				{
					Duct duct3;
					if (terminalConnType == 0)
					{
						duct3 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
						this.DuctSetParameter(validConnector, duct3, false);
						document.Regenerate();
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
						if (!hypiping.IsYAxis())
						{
							double xaxisAngle = hypiping.GetXAxisAngle();
							hypiping2.Rotate(Math.PI - xaxisAngle);
						}
						Connector closeConnector = hypiping2.GetCloseConnector(validConnector.Origin, false);
						if (!this.IsConnectSameDirection(validConnector, closeConnector))
						{
							hypiping2.Rotate(Math.PI*.5);
						}
						closeConnector.ConnectTo(validConnector);
						document.Regenerate();
					}
					else
					{
						duct3 = this.FlexDuctConnection(document, curveIntersection, validConnector, hypiping);
					}
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
					document.Create.NewTakeoffFitting(hypiping3.GetAppropriateConnector(curveIntersection), hypiping.Piping);
					duct2 = hypiping3.Piping;
				}
				else
				{
					int num = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
					if (ductAirTerminalPoint != null && ductAirTerminalPoint.Count > 1)
					{
						Duct duct4;
						if (terminalConnType == 0)
						{
							duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
							this.DuctSetParameter(validConnector, duct4, false);
							document.Regenerate();
							YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct4);
							Connector closeConnector2 = hypiping4.GetCloseConnector(validConnector.Origin, false);
							if (!this.IsConnectSameDirection(validConnector, closeConnector2))
							{
								hypiping4.Rotate(Math.PI*.5);
							}
							closeConnector2.ConnectTo(validConnector);
							document.Regenerate();
						}
						else
						{
							duct4 = this.FlexDuctConnection(document, ductAirTerminalPoint[1], validConnector, hypiping);
						}
						Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct5, false);
						document.Regenerate();
						YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct4);
						YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(duct5);
						if (!hypiping.IsYAxis())
						{
							double xaxisAngle2 = hypiping.GetXAxisAngle();
							hypiping5.Rotate(Math.PI - xaxisAngle2);
						}
						if (hypiping6.IsXAxis() && hypiping6.IsRect())
						{
							hypiping6.Rotate90();
						}
						document.Create.NewElbowFitting(hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[1]));
						if (num == 1)
						{
							document.Create.NewTakeoffFitting(hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping.Piping);
						}
						else
						{
							document.Create.NewElbowFitting(hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]));
						}
						duct2 = hypiping6.Piping;
					}
				}
				subTransaction.Commit();
				document.Regenerate();
				result = duct2;
			}
			catch (Exception)
			{
				duct2 = null;
				result = duct2;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private IList<XYZ> GetDuctAirTerminalPoint(XYZ airTerminalOrigin, XYZ airTerminalLocationPoint, YJKPiping<Duct> hyDuct, ref int conType)
		{
			IList<XYZ> list = new List<XYZ>();
			IntersectionResult intersectionResult = hyDuct.GetLocationCurve().Project(airTerminalLocationPoint);
			Line line = YJKLineEx.YJKGetBound(hyDuct.StartConnector.Origin, hyDuct.EndConnector.Origin);
			line.MakeUnbound();
			IntersectionResult intersectionResult2 = line.Project(airTerminalLocationPoint);
			list.Add(intersectionResult.XYZPoint);
			Line line2 = line as Line;
			Line line3 = YJKLineEx.YJKGetBound(airTerminalOrigin, airTerminalLocationPoint);
			XYZ xyz = line2.Direction.CrossProduct(line3.Direction);
			Line line4 = YJKLineEx.YJKGetBound(intersectionResult.XYZPoint, intersectionResult.XYZPoint + xyz);
			line3.MakeUnbound();
			line4.MakeUnbound();
			IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
			SetComparisonResult setComparisonResult = line3.Intersect(line4, out intersectionResultArray);
			if (4 != (int)setComparisonResult && !intersectionResultArray.IsEmpty)
			{
				list.Add(intersectionResultArray.get_Item(0).XYZPoint);
			}
			XYZ xyzpoint = intersectionResult.XYZPoint;
			if (xyzpoint.IsAlmostEqualTo(intersectionResult2.XYZPoint))
			{
				if (xyzpoint.IsAlmostEqualTo(hyDuct.StartConnector.Origin) || xyzpoint.IsAlmostEqualTo(hyDuct.EndConnector.Origin) || xyzpoint.DistanceTo(hyDuct.StartConnector.Origin) < 0.01 || xyzpoint.DistanceTo(hyDuct.EndConnector.Origin) < 0.01)
				{
					conType = 0;
				}
				else
				{
					conType = 1;
				}
			}
			else
			{
				conType = 0;
			}
			return list;
		}

		public bool DuctConnectAirTerminalTee(Duct duct, FamilyInstance airTerminal)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				if (validConnector != null && validConnector.IsConnected)
				{
					throw new Exception("风口已经有风管连接");
				}
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				bool flag;
				if (curveIntersection != null)
				{
					Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
					this.DuctSetParameter(validConnector, duct2, false);
					document.Regenerate();
					this.ConnNewDuctAndConnector(validConnector, duct2);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					IList<Duct> list2 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list2 != null)
					{
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list2[1]);
						document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection));
						flag = true;
					}
					else
					{
						flag = false;
					}
				}
				else
				{
					int num = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
					if (num != 1 || ductAirTerminalPoint == null || ductAirTerminalPoint.Count <= 1)
					{
						throw new Exception("三通连接失败，可能原因是风管长度不够");
					}
					Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
					this.DuctSetParameter(validConnector, duct3, false);
					Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
					this.DuctSetParameter(validConnector, duct4, false);
					document.Regenerate();
					this.ConnNewDuctAndConnector(validConnector, duct3);
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct3);
					YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct4);
					if (hypiping5.IsXAxis() && hypiping5.IsRect())
					{
						hypiping5.Rotate90();
					}
					document.Create.NewElbowFitting(hypiping4.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]));
					IList<Duct> list3 = hypiping.Separate(ductAirTerminalPoint[0]);
					document.Regenerate();
					if (list3 != null)
					{
						YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(list3[1]);
						document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping5.GetAppropriateConnector(ductAirTerminalPoint[0]));
						flag = true;
					}
					else
					{
						flag = false;
					}
				}
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctConnectAirTerminalTee(Duct duct, FamilyInstance airTerminal, ref string connMSG)
		{
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				if (validConnector != null && validConnector.IsConnected)
				{
					connMSG = "风口已经有风管连接";
					result = false;
				}
				else
				{
					IList<XYZ> list = new List<XYZ>();
					list.Add(validConnector.Origin);
					list.Add(this.GetConnectorDirectionPoint(validConnector));
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
					Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
					Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
					curve.MakeUnbound();
					curve2.MakeUnbound();
					XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
					bool flag;
					if (curveIntersection != null)
					{
						Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
						this.DuctSetParameter(validConnector, duct2, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector, duct2);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
						IList<Duct> list2 = hypiping.Separate(curveIntersection);
						document.Regenerate();
						if (list2 != null)
						{
							YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list2[1]);
							document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection));
							flag = true;
						}
						else
						{
							flag = false;
						}
					}
					else
					{
						int num = 1;
						IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
						if (num != 1 || ductAirTerminalPoint == null || ductAirTerminalPoint.Count <= 1)
						{
							connMSG = "三通连接失败，可能原因是风管长度不够";
							return false;
						}
						Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
						this.DuctSetParameter(validConnector, duct3, false);
						Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct4, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector, duct3);
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct3);
						YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct4);
						if (hypiping5.IsXAxis() && hypiping5.IsRect())
						{
							hypiping5.Rotate90();
						}
						document.Create.NewElbowFitting(hypiping4.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]));
						IList<Duct> list3 = hypiping.Separate(ductAirTerminalPoint[0]);
						document.Regenerate();
						if (list3 != null)
						{
							YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(list3[1]);
							document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping5.GetAppropriateConnector(ductAirTerminalPoint[0]));
							flag = true;
						}
						else
						{
							flag = false;
						}
					}
					result = flag;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public Connector GetValidConnector(FamilyInstance airTerminal)
		{
			Connector result = null;
			if (airTerminal == null)
			{
				return result;
			}
			foreach (Connector connector in ((IEnumerable<Connector>)YJKRevitTools.GetElementConnector(airTerminal, (Autodesk.Revit.DB.Domain)1)))
			{
                if ((int)connector.Shape != -1)
				{
					result = connector;
				}
			}
			return result;
		}

		public bool ConnectAirTerminalByTee(Duct duct, FamilyInstance airTerminal, XYZ globalPoint, ref string connMSG, int alignType)
		{
			bool result = false;
			connMSG = string.Empty;
			SubTransaction subTransaction = null;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				IList<Duct> list2 = new List<Duct>();
				List<string> list3 = new List<string>();
				IList<XYZ> list4 = new List<XYZ>();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				ConnectDuctsByFitting connectDuctsByFitting = new ConnectDuctsByFitting();
				string connectorSharpName = YJKRevitTools.GetConnectorSharpName(hypiping.StartConnector.Connector);
				if (curveIntersection != null)
				{
					Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
					document.Regenerate();
					this.DuctSetParameter(validConnector, duct2, false);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle = hypiping.GetXAxisAngle();
						hypiping2.Rotate(Math.PI - xaxisAngle);
					}
					Connector closeConnector = hypiping2.GetCloseConnector(validConnector.Origin, false);
					if (!this.IsConnectSameDirection(validConnector, closeConnector))
					{
						hypiping2.Rotate(Math.PI*.5);
					}
					closeConnector.ConnectTo(validConnector);
					IList<Duct> list5 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list5 != null)
					{
						hyrotateFamilyInstance.MovePiping(list5[0]);
						hyrotateFamilyInstance.MovePiping(list5[1]);
						list2 = new List<Duct>();
						list4 = new List<XYZ>();
						list2 = this.OrderSplitDucts(list5, globalPoint);
						list2.Add(duct2);
						list3 = new List<string>();
						hypiping = new YJKPiping<Duct>(list2[0]);
						list4.Add(YJKRevitTools.GetElementCurve(list2[0], 0) + YJKRevitTools.GetElementCurve(list2[0], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[1], 0) + YJKRevitTools.GetElementCurve(list2[1], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[2], 0) + YJKRevitTools.GetElementCurve(list2[2], 1) / 2.0);
						if (connectorSharpName == "矩形")
						{
							list3 = new ConnectDuctManage().GetRoundAngleTeeNames();
							connectDuctsByFitting.IsConnecteddByRoundAngleTee(list2, list4, list3, ref connMSG);
						}
						else
						{
							if (!(connectorSharpName == "圆形"))
							{
								throw new Exception("目前不支持椭圆形风管及风口连接！");
							}
							list3.Add("圆形直角三通-顶对齐");
							list3.Add("圆形直角三通-中心对齐");
							list3.Add("圆形直角三通-底对齐");
							if (YJKRevitTools.IsRiserDuct(duct2))
							{
								this.NewTeeFittingHT(list2, list4, list3, ref connMSG);
							}
							else
							{
								this.NewTeeFittingT(list2, list4, list3, ref connMSG);
							}
						}
						new YJKPiping<Duct>(list2[1]);
						document.Regenerate();
					}
				}
				else
				{
					int num = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
					Duct piping = null;
					Duct duct3 = null;
					this.CreatDuctsTerminalToDuct(ref piping, ref duct3, hypiping, validConnector, ductAirTerminalPoint, 0, alignType);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(piping);
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct3);
					if (hypiping4.IsXAxis() && hypiping4.IsRect())
					{
						hypiping4.Rotate90();
						this.AlignDucts(hypiping.Piping, duct3, alignType);
					}
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle2 = hypiping.GetXAxisAngle();
						hypiping3.Rotate(Math.PI - xaxisAngle2);
					}
					this.ConnElbowDucts(hypiping3, hypiping4, ductAirTerminalPoint[1], document);
					if (num != 1)
					{
						this.ConnElbowDucts(hypiping, hypiping4, ductAirTerminalPoint[0], document);
					}
					else
					{
						IList<Duct> list6 = hypiping.Separate(ductAirTerminalPoint[0]);
						document.Regenerate();
						hyrotateFamilyInstance.MovePiping(list6[0]);
						hyrotateFamilyInstance.MovePiping(list6[1]);
						list2 = new List<Duct>();
						list4 = new List<XYZ>();
						list2 = this.OrderSplitDucts(list6, globalPoint);
						list2.Add(duct3);
						list3 = new List<string>();
						hypiping = new YJKPiping<Duct>(list2[0]);
						list4.Add(YJKRevitTools.GetElementCurve(list2[0], 0) + YJKRevitTools.GetElementCurve(list2[0], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[1], 0) + YJKRevitTools.GetElementCurve(list2[1], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[2], 0) + YJKRevitTools.GetElementCurve(list2[2], 1) / 2.0);
						if (connectorSharpName == "矩形")
						{
							list3 = new ConnectDuctManage().GetRoundAngleTeeNames();
							connectDuctsByFitting.IsConnecteddByRoundAngleTee(list2, list4, list3, ref connMSG);
						}
						else
						{
							if (!(connectorSharpName == "圆形"))
							{
								throw new Exception("目前不支持椭圆形风管及风口连接！");
							}
							list3.Add("圆形直角三通-顶对齐");
							list3.Add("圆形直角三通-中心对齐");
							list3.Add("圆形直角三通-底对齐");
							if (YJKRevitTools.IsRiserDuct(duct3))
							{
								this.NewTeeFittingHT(list2, list4, list3, ref connMSG);
							}
							else
							{
								this.NewTeeFittingT(list2, list4, list3, ref connMSG);
							}
						}
						new YJKPiping<Duct>(list2[1]);
						document.Regenerate();
						Duct piping2 = hypiping4.Piping;
						hyrotateFamilyInstance.MovePiping(piping2);
					}
				}
				document.Regenerate();
				subTransaction.Commit();
				result = true;
			}
			catch (Exception ex)
			{
				result = false;
				string message = ex.Message;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public IList<Duct> OrderSplitDucts(IList<Duct> mainDucts, XYZ globalPoint)
		{
			IList<Duct> list = new List<Duct>();
			if (mainDucts == null || mainDucts.Count < 2)
			{
				return list;
			}
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(mainDucts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(mainDucts[1]);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			YJKLine hyline2 = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
			hyline2.MakeUnBound();
			XYZ project = hyline.GetProject(globalPoint);
			XYZ project2 = hyline2.GetProject(globalPoint);
			if (hyline.IsPointOnLine(project))
			{
				list.Add(mainDucts[0]);
				list.Add(mainDucts[1]);
			}
			else if (hyline2.IsPointOnLine(project2))
			{
				list.Add(mainDucts[1]);
				list.Add(mainDucts[0]);
			}
			else
			{
				XYZ xyz = (YJKRevitTools.GetElementCurve(mainDucts[0], 0) + YJKRevitTools.GetElementCurve(mainDucts[0], 1)) / 2.0;
				XYZ xyz2 = (YJKRevitTools.GetElementCurve(mainDucts[1], 0) + YJKRevitTools.GetElementCurve(mainDucts[1], 1)) / 2.0;
				if (xyz.DistanceTo(globalPoint) <= xyz2.DistanceTo(globalPoint))
				{
					list.Add(mainDucts[0]);
					list.Add(mainDucts[1]);
				}
				else
				{
					list.Add(mainDucts[1]);
					list.Add(mainDucts[0]);
				}
			}
			return list;
		}

		public bool ConnectAirTerminalByTee(Duct duct, FamilyInstance airTerminal, ref string connMSG)
		{
			bool result = false;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				if (validConnector != null && validConnector.IsConnected)
				{
					connMSG = "风口已经有风管连接";
					return result;
				}
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				hyline.MakeUnBound();
				XYZ project = hyline.GetProject(validConnector.Origin);
				double num = hypiping.GetDiameter() * 1.5;
				double num2 = hypiping.StartConnector.Origin.DistanceTo(project);
				double num3 = hypiping.EndConnector.Origin.DistanceTo(project);
				if (hyline.IsPointOnLineExtension(project) || num2 < num || num3 < num)
				{
					connMSG = "风口与风管位置不能构成三通连接，\n请检查风口位置，用弯头或其他方式进行连接！";
					return result;
				}
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2, 0.00984251968503937);
				if (validConnector.IsConnected)
				{
					connMSG = "风口已连接！";
					return result;
				}
				double num4 = 0.017453292519943295;
				if (hypiping.StartConnector.Connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(validConnector.CoordinateSystem.BasisZ, num4) || hypiping.EndConnector.Connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(validConnector.CoordinateSystem.BasisZ, num4))
				{
					connMSG = "风口与风管不能构成三通连接关系！";
					return result;
				}
				if (curveIntersection != null)
				{
					Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
					this.DuctSetParameter(validConnector, duct2, false);
					document.Regenerate();
					this.ConnNewDuctAndConnector(validConnector, duct2);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle = hypiping.GetXAxisAngle();
						hypiping2.Rotate(Math.PI*.5 - xaxisAngle);
					}
					IList<Duct> list2 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list2 != null)
					{
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list2[1]);
						document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection));
						result = true;
						return result;
					}
					connMSG = "风口位置不能构成三通连接位置关系！";
					return result;
				}
				else
				{
					XYZ dlineP = YJKRevitTools.GetDLineP(list[0], list[1], hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
					XYZ dlineP2 = YJKRevitTools.GetDLineP(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin, list[0], list[1]);
					if (dlineP2.DistanceTo(dlineP) < hypiping.GetDiameter() * 2.0)
					{
						connMSG = "风口位置距风管位置太近，\n无法构成三通连接！";
						return result;
					}
					Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, dlineP2, validConnector.Origin);
					this.DuctSetParameter(validConnector, duct3, false);
					Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, dlineP, dlineP2);
					this.DuctSetParameter(validConnector, duct4, false);
					document.Regenerate();
					this.ConnNewDuctAndConnector(validConnector, duct3);
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct3);
					YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct4);
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle2 = hypiping.GetXAxisAngle();
						hypiping4.Rotate(Math.PI*.5 - xaxisAngle2);
					}
					if (hypiping5.IsXAxis() && hypiping5.IsRect())
					{
						hypiping5.Rotate90();
					}
					document.Create.NewElbowFitting(hypiping4.GetAppropriateConnector(dlineP2), hypiping5.GetAppropriateConnector(dlineP2));
					IList<Duct> list3 = hypiping.Separate(dlineP);
					document.Regenerate();
					if (list3 != null)
					{
						YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(list3[1]);
						document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(dlineP), hypiping6.GetAppropriateConnector(dlineP), hypiping5.GetAppropriateConnector(dlineP));
						result = true;
					}
				}
			}
			catch (Exception ex)
			{
				connMSG = ex.Message;
				result = false;
				return result;
			}
			return result;
		}

		private Duct CheckDuctEndPoint(YJKPiping<Duct> hyDuct, YJKPiping<Duct> hyDuct2, ref XYZ endPoint)
		{
			double num = hyDuct.StartConnector.Origin.DistanceTo(endPoint) + hyDuct.EndConnector.Origin.DistanceTo(endPoint);
			double num2 = hyDuct2.StartConnector.Origin.DistanceTo(endPoint) + hyDuct2.EndConnector.Origin.DistanceTo(endPoint);
			Duct piping;
			if (num - num2 < 0.0001)
			{
				piping = hyDuct2.Piping;
				if (hyDuct2.StartConnector.Origin.DistanceTo(endPoint) > hyDuct2.EndConnector.Origin.DistanceTo(endPoint))
				{
					endPoint = hyDuct2.EndConnector.Origin;
				}
				else
				{
					endPoint = hyDuct2.StartConnector.Origin;
				}
			}
			else
			{
				piping = hyDuct.Piping;
				if (hyDuct.StartConnector.Origin.DistanceTo(endPoint) > hyDuct.EndConnector.Origin.DistanceTo(endPoint))
				{
					endPoint = hyDuct.EndConnector.Origin;
				}
				else
				{
					endPoint = hyDuct.StartConnector.Origin;
				}
			}
			return piping;
		}

		private Duct CheckDuctEndPoint(YJKPiping<Duct> hyDuct, ref XYZ endPoint)
		{
			Duct piping = hyDuct.Piping;
			if (hyDuct.StartConnector.Origin.DistanceTo(endPoint) < hyDuct.EndConnector.Origin.DistanceTo(endPoint))
			{
				endPoint = hyDuct.StartConnector.Origin;
				return piping;
			}
			endPoint = hyDuct.EndConnector.Origin;
			return piping;
		}

		public Duct DuctConnectAirTerminalTee(ref Duct duct, FamilyInstance airTerminal, ref XYZ endPoint, int terminalConnType)
		{
			Duct duct2 = null;
			SubTransaction subTransaction = null;
			Duct result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				if (curveIntersection != null)
				{
					Duct duct3;
					if (terminalConnType == 0)
					{
						duct3 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
					}
					else
					{
						duct3 = this.FlexDuctConnection(document, curveIntersection, validConnector, hypiping);
					}
					document.Regenerate();
					this.DuctSetParameter(validConnector, duct3, false);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
					IList<Duct> list2 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list2 != null)
					{
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list2[1]);
						document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection));
						document.Regenerate();
						duct2 = hypiping2.Piping;
						duct = this.CheckDuctEndPoint(hypiping, hypiping3, ref endPoint);
					}
					else
					{
						duct2 = null;
					}
				}
				else
				{
					int num = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
					if (ductAirTerminalPoint != null && ductAirTerminalPoint.Count > 1)
					{
						Duct duct4;
						if (terminalConnType == 0)
						{
							duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
						}
						else
						{
							duct4 = this.FlexDuctConnection(document, ductAirTerminalPoint[1], validConnector, hypiping);
						}
						document.Regenerate();
						this.DuctSetParameter(validConnector, duct4, false);
						Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct5, false);
						document.Regenerate();
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct4);
						YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct5);
						if (hypiping5.IsXAxis() && hypiping5.IsRect())
						{
							hypiping5.Rotate90();
						}
						document.Create.NewElbowFitting(hypiping4.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]));
						if (num == 1)
						{
							IList<Duct> list3 = hypiping.Separate(ductAirTerminalPoint[0]);
							document.Regenerate();
							if (list3 != null)
							{
								YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(list3[1]);
								document.Create.NewTeeFitting(hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping5.GetAppropriateConnector(ductAirTerminalPoint[0]));
								document.Regenerate();
								duct2 = hypiping5.Piping;
								duct = this.CheckDuctEndPoint(hypiping, hypiping6, ref endPoint);
							}
							else
							{
								duct2 = null;
							}
						}
						else
						{
							document.Create.NewElbowFitting(hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping5.GetAppropriateConnector(ductAirTerminalPoint[0]));
							duct2 = hypiping5.Piping;
							duct = this.CheckDuctEndPoint(hypiping, ref endPoint);
						}
					}
				}
				document.Regenerate();
				subTransaction.Commit();
				result = duct2;
			}
			catch (Exception)
			{
				duct2 = null;
				result = duct2;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public Duct DuctTeeConnTerminal(ref Duct duct, FamilyInstance airTerminal, ref XYZ endPoint, int terminalConnType, XYZ originPnt, int alignType)
		{
			SubTransaction subTransaction = null;
			Duct result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				IList<Duct> list2 = new List<Duct>();
				List<string> list3 = new List<string>();
				IList<XYZ> list4 = new List<XYZ>();
				string text = "";
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				ConnectDuctsByFitting connectDuctsByFitting = new ConnectDuctsByFitting();
				string connectorSharpName = YJKRevitTools.GetConnectorSharpName(hypiping.StartConnector.Connector);
				Duct duct3;
				if (curveIntersection != null)
				{
					Duct duct2;
					if (terminalConnType == 0)
					{
						duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
						this.DuctSetParameter(validConnector, duct2, false);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
						if (!hypiping.IsYAxis())
						{
							double xaxisAngle = hypiping.GetXAxisAngle();
							hypiping2.Rotate(Math.PI - xaxisAngle);
						}
						Connector closeConnector = hypiping2.GetCloseConnector(validConnector.Origin, false);
						if (!this.IsConnectSameDirection(validConnector, closeConnector))
						{
							hypiping2.Rotate(Math.PI*.5);
						}
						closeConnector.ConnectTo(validConnector);
						document.Regenerate();
					}
					else
					{
						duct2 = this.FlexDuctConnection(document, curveIntersection, validConnector, hypiping);
					}
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct2);
					IList<Duct> list5 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list5 != null)
					{
						hyrotateFamilyInstance.MovePiping(list5[0]);
						hyrotateFamilyInstance.MovePiping(list5[1]);
						list2 = new List<Duct>();
						list4 = new List<XYZ>();
						if (this.CompareDuctDistance(list5[0], list5[1], originPnt))
						{
							list2.Add(list5[0]);
							list2.Add(list5[1]);
						}
						else
						{
							list2.Add(list5[1]);
							list2.Add(list5[0]);
						}
						list2.Add(duct2);
						list3 = new List<string>();
						hypiping = new YJKPiping<Duct>(list2[0]);
						list4.Add(YJKRevitTools.GetElementCurve(list2[0], 0) + YJKRevitTools.GetElementCurve(list2[0], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[1], 0) + YJKRevitTools.GetElementCurve(list2[1], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[2], 0) + YJKRevitTools.GetElementCurve(list2[2], 1) / 2.0);
						if (connectorSharpName == "矩形")
						{
							list3 = new ConnectDuctManage().GetRoundAngleTeeNames();
							connectDuctsByFitting.IsConnecteddByRoundAngleTee(list2, list4, list3, ref text);
						}
						else
						{
							if (!(connectorSharpName == "圆形"))
							{
								throw new Exception("目前不支持椭圆形风管及风口连接！");
							}
							list3.Add("圆形直角三通-顶对齐");
							list3.Add("圆形直角三通-中心对齐");
							list3.Add("圆形直角三通-底对齐");
							if (YJKRevitTools.IsRiserDuct(duct2))
							{
								this.NewTeeFittingHT(list2, list4, list3, ref text);
							}
							else
							{
								this.NewTeeFittingT(list2, list4, list3, ref text);
							}
						}
						YJKPiping<Duct> hyDuct = new YJKPiping<Duct>(list2[1]);
						document.Regenerate();
						duct3 = hypiping3.Piping;
						hyrotateFamilyInstance.MovePiping(duct3);
						duct = this.CheckDuctEndPoint(hypiping, hyDuct, ref endPoint);
						hyrotateFamilyInstance.MovePiping(duct);
					}
					else
					{
						duct3 = null;
					}
				}
				else
				{
					int num = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
					if (ductAirTerminalPoint == null || ductAirTerminalPoint.Count < 2)
					{
						return null;
					}
					Duct piping = null;
					Duct duct4 = null;
					this.CreatDuctsTerminalToDuct(ref piping, ref duct4, hypiping, validConnector, ductAirTerminalPoint, terminalConnType, alignType);
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(piping);
					YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct4);
					if (hypiping5.IsXAxis() && hypiping5.IsRect())
					{
						hypiping5.Rotate90();
						this.AlignDucts(hypiping.Piping, duct4, alignType);
					}
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle2 = hypiping.GetXAxisAngle();
						hypiping4.Rotate(Math.PI - xaxisAngle2);
					}
					this.ConnElbowDucts(hypiping4, hypiping5, ductAirTerminalPoint[1], document);
					if (num != 1)
					{
						document.Create.NewElbowFitting(hypiping.GetNearConnectorToPoint(ductAirTerminalPoint[0]), hypiping5.GetNearConnectorToPoint(ductAirTerminalPoint[0]));
						duct3 = hypiping5.Piping;
						duct = this.CheckDuctEndPoint(hypiping, ref endPoint);
					}
					else
					{
						IList<Duct> list6 = hypiping.Separate(ductAirTerminalPoint[0]);
						document.Regenerate();
						if (list6 == null)
						{
							return null;
						}
						hyrotateFamilyInstance.MovePiping(list6[0]);
						hyrotateFamilyInstance.MovePiping(list6[1]);
						list2 = new List<Duct>();
						list4 = new List<XYZ>();
						if (this.CompareDuctDistance(list6[0], list6[1], originPnt))
						{
							list2.Add(list6[0]);
							list2.Add(list6[1]);
						}
						else
						{
							list2.Add(list6[1]);
							list2.Add(list6[0]);
						}
						list2.Add(duct4);
						list3 = new List<string>();
						hypiping = new YJKPiping<Duct>(list2[0]);
						list4.Add(YJKRevitTools.GetElementCurve(list2[0], 0) + YJKRevitTools.GetElementCurve(list2[0], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[1], 0) + YJKRevitTools.GetElementCurve(list2[1], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[2], 0) + YJKRevitTools.GetElementCurve(list2[2], 1) / 2.0);
						if (connectorSharpName == "矩形")
						{
							list3 = new ConnectDuctManage().GetRoundAngleTeeNames();
							connectDuctsByFitting.IsConnecteddByRoundAngleTee(list2, list4, list3, ref text);
						}
						else
						{
							if (!(connectorSharpName == "圆形"))
							{
								throw new Exception("目前不支持椭圆形风管及风口连接！");
							}
							list3.Add("圆形直角三通-顶对齐");
							list3.Add("圆形直角三通-中心对齐");
							list3.Add("圆形直角三通-底对齐");
							if (YJKRevitTools.IsRiserDuct(duct4))
							{
								this.NewTeeFittingHT(list2, list4, list3, ref text);
							}
							else
							{
								this.NewTeeFittingT(list2, list4, list3, ref text);
							}
						}
						YJKPiping<Duct> hyDuct2 = new YJKPiping<Duct>(list2[1]);
						document.Regenerate();
						duct3 = hypiping5.Piping;
						hyrotateFamilyInstance.MovePiping(duct3);
						duct = this.CheckDuctEndPoint(hypiping, hyDuct2, ref endPoint);
						hyrotateFamilyInstance.MovePiping(duct);
					}
				}
				document.Regenerate();
				subTransaction.Commit();
				result = duct3;
			}
			catch (Exception)
			{
				Duct duct3 = null;
				result = duct3;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public Duct DuctConnectAirTerminalTee(ref Duct duct, FamilyInstance airTerminal, ref XYZ endPoint, int terminalConnType, XYZ originPnt)
		{
			Duct duct2 = null;
			SubTransaction subTransaction = null;
			Duct result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				IList<Duct> list2 = new List<Duct>();
				List<string> list3 = new List<string>();
				IList<XYZ> list4 = new List<XYZ>();
				string text = "";
				XYZ xyz = this.GetCurveIntersection(curve, curve2);
				XYZ xyz2 = validConnector.Origin;
				if (xyz == null)
				{
					XYZ project = hypiping.GetProject(validConnector.Origin);
					XYZ xyz3 = (project - validConnector.Origin).Normalize();
					XYZ xyz4 = new XYZ(0.0, 0.0, 1.0);
					if (xyz3.IsAlmostEqualTo(xyz4, 0.001) || xyz3.IsAlmostEqualTo(-xyz4, 0.001))
					{
						xyz = project;
						xyz2 = new XYZ(xyz.X, xyz.Y, validConnector.Origin.Z);
					}
				}
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				ConnectDuctsByFitting connectDuctsByFitting = new ConnectDuctsByFitting();
				string connectorSharpName = YJKRevitTools.GetConnectorSharpName(hypiping.StartConnector.Connector);
				if (xyz != null)
				{
					Duct duct3;
					if (terminalConnType == 0)
					{
						duct3 = YJKRevitTools.NewDuct(hypiping.Piping, xyz, xyz2);
					}
					else
					{
						duct3 = this.FlexDuctConnection(document, xyz, validConnector, hypiping);
					}
					document.Regenerate();
					this.DuctSetParameter(validConnector, duct3, false);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle = hypiping.GetXAxisAngle();
						hypiping2.Rotate(Math.PI - xaxisAngle);
					}
					Connector closeConnector = hypiping2.GetCloseConnector(xyz2, false);
					if (!this.IsConnectSameDirection(validConnector, closeConnector))
					{
						hypiping2.Rotate(Math.PI*.5);
					}
					if (!closeConnector.IsConnected)
					{
						closeConnector.ConnectTo(validConnector);
					}
					IList<Duct> list5 = hypiping.Separate(xyz);
					document.Regenerate();
					if (list5 != null)
					{
						hyrotateFamilyInstance.MovePiping(list5[0]);
						hyrotateFamilyInstance.MovePiping(list5[1]);
						list2 = new List<Duct>();
						list4 = new List<XYZ>();
						if (this.CompareDuctDistance(list5[0], list5[1], originPnt))
						{
							list2.Add(list5[0]);
							list2.Add(list5[1]);
						}
						else
						{
							list2.Add(list5[1]);
							list2.Add(list5[0]);
						}
						list2.Add(duct3);
						list3 = new List<string>();
						hypiping = new YJKPiping<Duct>(list2[0]);
						list4.Add(YJKRevitTools.GetElementCurve(list2[0], 0) + YJKRevitTools.GetElementCurve(list2[0], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[1], 0) + YJKRevitTools.GetElementCurve(list2[1], 1) / 2.0);
						list4.Add(YJKRevitTools.GetElementCurve(list2[2], 0) + YJKRevitTools.GetElementCurve(list2[2], 1) / 2.0);
						if (connectorSharpName == "矩形")
						{
							list3 = new ConnectDuctManage().GetRoundAngleTeeNames();
							connectDuctsByFitting.IsConnecteddByRoundAngleTee(list2, list4, list3, ref text);
						}
						else
						{
							if (!(connectorSharpName == "圆形"))
							{
								throw new Exception("目前不支持椭圆形风管及风口连接！");
							}
							list3.Add("圆形斜角三通-顶对齐");
							list3.Add("圆形斜角三通-中心对齐");
							list3.Add("圆形斜角三通-底对齐");
							if (YJKRevitTools.IsRiserDuct(duct3))
							{
								this.NewTeeFittingHT(list2, list4, list3, ref text);
							}
							else
							{
								this.NewTeeFittingT(list2, list4, list3, ref text);
							}
						}
						YJKPiping<Duct> hyDuct = new YJKPiping<Duct>(list2[1]);
						document.Regenerate();
						duct2 = hypiping2.Piping;
						hyrotateFamilyInstance.MovePiping(duct2);
						duct = this.CheckDuctEndPoint(hypiping, hyDuct, ref endPoint);
						hyrotateFamilyInstance.MovePiping(duct);
					}
					else
					{
						duct2 = null;
					}
				}
				else
				{
					int num = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
					if (ductAirTerminalPoint != null && ductAirTerminalPoint.Count > 1)
					{
						Duct duct4;
						if (terminalConnType == 0)
						{
							duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
						}
						else
						{
							duct4 = this.FlexDuctConnection(document, ductAirTerminalPoint[1], validConnector, hypiping);
						}
						document.Regenerate();
						this.DuctSetParameter(validConnector, duct4, false);
						Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct5, false);
						document.Regenerate();
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct4);
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct5);
						if (hypiping4.IsXAxis() && hypiping4.IsRect())
						{
							hypiping4.Rotate90();
						}
						if (!hypiping.IsYAxis())
						{
							double xaxisAngle2 = hypiping.GetXAxisAngle();
							hypiping3.Rotate(Math.PI - xaxisAngle2);
						}
						Connector closeConnector2 = hypiping3.GetCloseConnector(validConnector.Origin, false);
						if (!this.IsConnectSameDirection(validConnector, closeConnector2))
						{
							hypiping3.Rotate(Math.PI*.5);
						}
						if (!closeConnector2.IsConnected)
						{
							closeConnector2.ConnectTo(validConnector);
						}
						document.Create.NewElbowFitting(hypiping3.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping4.GetAppropriateConnector(ductAirTerminalPoint[1]));
						if (num == 1)
						{
							IList<Duct> list6 = hypiping.Separate(ductAirTerminalPoint[0]);
							document.Regenerate();
							if (list6 != null)
							{
								hyrotateFamilyInstance.MovePiping(list6[0]);
								hyrotateFamilyInstance.MovePiping(list6[1]);
								list2 = new List<Duct>();
								list4 = new List<XYZ>();
								if (this.CompareDuctDistance(list6[0], list6[1], originPnt))
								{
									list2.Add(list6[0]);
									list2.Add(list6[1]);
								}
								else
								{
									list2.Add(list6[1]);
									list2.Add(list6[0]);
								}
								list2.Add(duct5);
								list3 = new List<string>();
								hypiping = new YJKPiping<Duct>(list2[0]);
								list4.Add(YJKRevitTools.GetElementCurve(list2[0], 0) + YJKRevitTools.GetElementCurve(list2[0], 1) / 2.0);
								list4.Add(YJKRevitTools.GetElementCurve(list2[1], 0) + YJKRevitTools.GetElementCurve(list2[1], 1) / 2.0);
								list4.Add(YJKRevitTools.GetElementCurve(list2[2], 0) + YJKRevitTools.GetElementCurve(list2[2], 1) / 2.0);
								if (connectorSharpName == "矩形")
								{
									list3 = new ConnectDuctManage().GetRoundAngleTeeNames();
									connectDuctsByFitting.IsConnecteddByRoundAngleTee(list2, list4, list3, ref text);
								}
								else
								{
									if (!(connectorSharpName == "圆形"))
									{
										throw new Exception("目前不支持椭圆形风管及风口连接！");
									}
									list3.Add("圆形斜角三通-顶对齐");
									list3.Add("圆形斜角三通-中心对齐");
									list3.Add("圆形斜角三通-底对齐");
									if (YJKRevitTools.IsRiserDuct(duct5))
									{
										this.NewTeeFittingHT(list2, list4, list3, ref text);
									}
									else
									{
										this.NewTeeFittingT(list2, list4, list3, ref text);
									}
								}
								YJKPiping<Duct> hyDuct2 = new YJKPiping<Duct>(list2[1]);
								document.Regenerate();
								duct2 = hypiping4.Piping;
								hyrotateFamilyInstance.MovePiping(duct2);
								duct = this.CheckDuctEndPoint(hypiping, hyDuct2, ref endPoint);
								hyrotateFamilyInstance.MovePiping(duct);
							}
							else
							{
								duct2 = null;
							}
						}
						else
						{
							document.Create.NewElbowFitting(hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping4.GetAppropriateConnector(ductAirTerminalPoint[0]));
							duct2 = hypiping4.Piping;
							duct = this.CheckDuctEndPoint(hypiping, ref endPoint);
						}
					}
				}
				document.Regenerate();
				subTransaction.Commit();
				result = duct2;
			}
			catch (Exception)
			{
				duct2 = null;
				result = duct2;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private double GetDuctToPointDis(Duct duct, XYZ originPnt)
		{
			return YJKRevitTools.GetElementCurve(duct, 0).Add(YJKRevitTools.GetElementCurve(duct, 1)).DistanceTo(originPnt);
		}

		private bool CompareDuctDistance(Duct duct1, Duct duct2, XYZ basePoint)
		{
			bool result = true;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct1);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
			double num = hypiping.StartConnector.Origin.DistanceTo(basePoint) + hypiping.EndConnector.Origin.DistanceTo(basePoint);
			double num2 = hypiping2.StartConnector.Origin.DistanceTo(basePoint) + hypiping2.EndConnector.Origin.DistanceTo(basePoint);
			if (num - num2 > 0.001)
			{
				result = false;
			}
			return result;
		}

		public bool DuctConnectAirTerminalCross(Duct duct, FamilyInstance airTerminal, FamilyInstance airTerminal2)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				Connector validConnector2 = this.GetValidConnector(airTerminal2);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				if (validConnector != null && validConnector2 != null && (validConnector.IsConnected || validConnector2.IsConnected))
				{
					throw new Exception("风口已经有风管连接");
				}
				IList<XYZ> list = new List<XYZ>();
				IList<XYZ> list2 = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				list2.Add(validConnector2.Origin);
				list2.Add(this.GetConnectorDirectionPoint(validConnector2));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				Curve curve3 = YJKLineEx.YJKGetBound(list2[0], list2[1]);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				curve3.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				XYZ curveIntersection2 = this.GetCurveIntersection(curve3, curve2);
				if (curveIntersection != null && curveIntersection.IsAlmostEqualTo(curveIntersection2))
				{
					Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
					this.DuctSetParameter(validConnector, duct2, false);
					document.Regenerate();
					this.ConnNewDuctAndConnector(validConnector, duct2);
					Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector2.Origin);
					this.DuctSetParameter(validConnector2, duct3, false);
					document.Regenerate();
					this.ConnNewDuctAndConnector(validConnector2, duct3);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
					IList<Duct> list3 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list3 != null)
					{
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(list3[1]);
						document.Create.NewCrossFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping4.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection));
						flag = true;
					}
					else
					{
						flag = false;
					}
				}
				else if (curveIntersection == null && curveIntersection2 == null)
				{
					int num = 1;
					int num2 = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
					IList<XYZ> ductAirTerminalPoint2 = this.GetDuctAirTerminalPoint(list2[0], list2[1], hypiping, ref num2);
					if (ductAirTerminalPoint != null && ductAirTerminalPoint.Count > 1 && ductAirTerminalPoint2 != null && ductAirTerminalPoint2.Count > 1 && ductAirTerminalPoint[0].IsAlmostEqualTo(ductAirTerminalPoint2[0]))
					{
						Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
						this.DuctSetParameter(validConnector, duct4, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector, duct4);
						Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct5, false);
						Duct duct6 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint2[1], validConnector2.Origin);
						this.DuctSetParameter(validConnector2, duct6, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector2, duct6);
						Duct duct7 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint2[0], ductAirTerminalPoint2[1]);
						this.DuctSetParameter(validConnector2, duct7, false);
						document.Regenerate();
						YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct4);
						YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(duct5);
						YJKPiping<Duct> hypiping7 = new YJKPiping<Duct>(duct6);
						YJKPiping<Duct> hypiping8 = new YJKPiping<Duct>(duct7);
						if (hypiping6.IsXAxis() && hypiping6.IsRect())
						{
							hypiping6.Rotate90();
						}
						if (hypiping8.IsXAxis() && hypiping8.IsRect())
						{
							hypiping8.Rotate90();
						}
						document.Create.NewElbowFitting(hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[1]));
						document.Create.NewElbowFitting(hypiping7.GetAppropriateConnector(ductAirTerminalPoint2[1]), hypiping8.GetAppropriateConnector(ductAirTerminalPoint2[1]));
						if (num != 1 || num2 != 1)
						{
							throw new Exception("四通连接失败，可能原因是风管不相交");
						}
						IList<Duct> list4 = hypiping.Separate(ductAirTerminalPoint[0]);
						document.Regenerate();
						if (list4 != null)
						{
							YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(list4[1]);
							document.Create.NewCrossFitting(hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping9.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping8.GetAppropriateConnector(ductAirTerminalPoint[0]));
							flag = true;
						}
						else
						{
							flag = false;
						}
					}
				}
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private Duct CreatDuctAndConnTerminal(XYZ point, Connector conn, YJKPiping<Duct> hyDuct)
		{
			Autodesk.Revit.DB.Document document = hyDuct.Piping.Document;
			Duct duct = YJKRevitTools.NewDuct(hyDuct.Piping, point, conn.Origin);
			this.DuctSetParameter(conn, duct, false);
			document.Regenerate();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			if (!hyDuct.IsYAxis())
			{
				double xaxisAngle = hyDuct.GetXAxisAngle();
				hypiping.Rotate(Math.PI*.5 - xaxisAngle);
			}
			Connector nearConnectorToPoint = hypiping.GetNearConnectorToPoint(conn.Origin);
			if (!this.IsConnectSameDirection(conn, nearConnectorToPoint))
			{
				hypiping.Rotate(Math.PI*.5);
			}
			conn.ConnectTo(nearConnectorToPoint);
			return duct;
		}

		public bool DuctConnectAirTerminalCross(Duct duct, FamilyInstance airTerminal, FamilyInstance airTerminal2, ref string connMSG, int alignType, XYZ globalPoint)
		{
			bool result = false;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				Connector validConnector2 = this.GetValidConnector(airTerminal2);
				if (validConnector != null && validConnector2 != null && (validConnector.IsConnected || validConnector2.IsConnected))
				{
					connMSG = "风口已经有风管连接";
					return false;
				}
				IList<XYZ> list = new List<XYZ>();
				IList<XYZ> list2 = new List<XYZ>();
				list.Add(validConnector.Origin);
				list.Add(this.GetConnectorDirectionPoint(validConnector));
				list2.Add(validConnector2.Origin);
				list2.Add(this.GetConnectorDirectionPoint(validConnector2));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				Curve curve3 = YJKLineEx.YJKGetBound(list2[0], list2[1]);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				curve3.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				XYZ curveIntersection2 = this.GetCurveIntersection(curve3, curve2);
				string connectorSharpName = YJKRevitTools.GetConnectorSharpName(hypiping.StartConnector.Connector);
				if (curveIntersection != null && curveIntersection.IsAlmostEqualTo(curveIntersection2))
				{
					Duct piping = this.CreatDuctAndConnTerminal(curveIntersection, validConnector, hypiping);
					Duct piping2 = this.CreatDuctAndConnTerminal(curveIntersection, validConnector2, hypiping);
					YJKPiping<Duct> hyDuct = new YJKPiping<Duct>(piping);
					YJKPiping<Duct> hyDuct2 = new YJKPiping<Duct>(piping2);
					IList<Duct> list3 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (!hypiping.IsYAxis())
					{
						hypiping.GetXAxisAngle();
					}
					if (list3 != null)
					{
						list3 = this.OrderSplitDucts(list3, globalPoint);
						YJKPiping<Duct> hyDuct3 = new YJKPiping<Duct>(list3[0]);
						YJKPiping<Duct> hyDuct4 = new YJKPiping<Duct>(list3[1]);
						this.ConnCrossDucts(hyDuct3, hyDuct4, hyDuct, hyDuct2, connectorSharpName);
						result = true;
					}
					else
					{
						result = false;
					}
				}
				else if (curveIntersection == null && curveIntersection2 == null)
				{
					int num = 1;
					int num2 = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
					IList<XYZ> ductAirTerminalPoint2 = this.GetDuctAirTerminalPoint(list2[0], list2[1], hypiping, ref num2);
					if (ductAirTerminalPoint != null && ductAirTerminalPoint.Count > 1 && ductAirTerminalPoint2 != null && ductAirTerminalPoint2.Count > 1 && ductAirTerminalPoint[0].IsAlmostEqualTo(ductAirTerminalPoint2[0]))
					{
						Duct piping3 = this.CreatDuctAndConnTerminal(ductAirTerminalPoint[1], validConnector, hypiping);
						Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct2, false);
						Duct piping4 = this.CreatDuctAndConnTerminal(ductAirTerminalPoint2[1], validConnector2, hypiping);
						Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint2[0], ductAirTerminalPoint2[1]);
						this.DuctSetParameter(validConnector2, duct3, false);
						document.Regenerate();
						YJKPiping<Duct> hyDuct5 = new YJKPiping<Duct>(piping3);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
						YJKPiping<Duct> hyDuct6 = new YJKPiping<Duct>(piping4);
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
						if (!hypiping.IsYAxis())
						{
							hypiping.GetXAxisAngle();
						}
						if (hypiping2.IsXAxis() && hypiping2.IsRect())
						{
							hypiping2.Rotate90();
						}
						if (hypiping3.IsXAxis() && hypiping3.IsRect())
						{
							hypiping3.Rotate90();
						}
						this.ConnElbowDucts(hyDuct5, hypiping2, ductAirTerminalPoint[1], document);
						this.ConnElbowDucts(hyDuct6, hypiping3, ductAirTerminalPoint2[1], document);
						this.AlignDucts(hypiping.Piping, hypiping2.Piping, alignType);
						this.AlignDucts(hypiping.Piping, hypiping3.Piping, alignType);
						if (num != 1 || num2 != 1)
						{
							connMSG = "四通连接失败，可能原因是风管不相交";
							return false;
						}
						IList<Duct> list4 = hypiping.Separate(ductAirTerminalPoint[0]);
						document.Regenerate();
						if (list4 != null)
						{
							list4 = this.OrderSplitDucts(list4, globalPoint);
							YJKPiping<Duct> hyDuct7 = new YJKPiping<Duct>(list4[0]);
							YJKPiping<Duct> hyDuct8 = new YJKPiping<Duct>(list4[1]);
							this.ConnCrossDucts(hyDuct7, hyDuct8, hypiping2, hypiping3, connectorSharpName);
							result = true;
						}
						else
						{
							result = false;
						}
					}
				}
				return result;
			}
			catch (Exception ex)
			{
				result = false;
				string message = ex.Message;
			}
			return result;
		}

		public bool DuctConnectAirTerminalCross(Duct duct, FamilyInstance airTerminal, FamilyInstance airTerminal2, ref string connMSG)
		{
			bool flag = false;
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				Connector validConnector2 = this.GetValidConnector(airTerminal2);
				if (validConnector != null && validConnector2 != null && (validConnector.IsConnected || validConnector2.IsConnected))
				{
					connMSG = "风口已经有风管连接";
					result = false;
				}
				else
				{
					IList<XYZ> list = new List<XYZ>();
					IList<XYZ> list2 = new List<XYZ>();
					list.Add(validConnector.Origin);
					list.Add(this.GetConnectorDirectionPoint(validConnector));
					list2.Add(validConnector2.Origin);
					list2.Add(this.GetConnectorDirectionPoint(validConnector2));
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
					Curve curve = YJKLineEx.YJKGetBound(list[0], list[1]);
					Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
					Curve curve3 = YJKLineEx.YJKGetBound(list2[0], list2[1]);
					curve.MakeUnbound();
					curve2.MakeUnbound();
					curve3.MakeUnbound();
					XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
					XYZ curveIntersection2 = this.GetCurveIntersection(curve3, curve2);
					if (curveIntersection != null && curveIntersection.IsAlmostEqualTo(curveIntersection2))
					{
						Duct duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
						this.DuctSetParameter(validConnector, duct2, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector, duct2);
						Duct duct3 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector2.Origin);
						this.DuctSetParameter(validConnector2, duct3, false);
						document.Regenerate();
						this.ConnNewDuctAndConnector(validConnector2, duct3);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
						IList<Duct> list3 = hypiping.Separate(curveIntersection);
						document.Regenerate();
						if (!hypiping.IsYAxis())
						{
							double xaxisAngle = hypiping.GetXAxisAngle();
							hypiping2.Rotate(Math.PI*.5 - xaxisAngle);
							hypiping3.Rotate(Math.PI*.5 - xaxisAngle);
						}
						if (list3 != null)
						{
							YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(list3[1]);
							document.Create.NewCrossFitting(hypiping.GetAppropriateConnector(curveIntersection), hypiping4.GetAppropriateConnector(curveIntersection), hypiping2.GetAppropriateConnector(curveIntersection), hypiping3.GetAppropriateConnector(curveIntersection));
							flag = true;
						}
						else
						{
							flag = false;
						}
					}
					else if (curveIntersection == null && curveIntersection2 == null)
					{
						int num = 1;
						int num2 = 1;
						IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list[0], list[1], hypiping, ref num);
						IList<XYZ> ductAirTerminalPoint2 = this.GetDuctAirTerminalPoint(list2[0], list2[1], hypiping, ref num2);
						if (ductAirTerminalPoint != null && ductAirTerminalPoint.Count > 1 && ductAirTerminalPoint2 != null && ductAirTerminalPoint2.Count > 1 && ductAirTerminalPoint[0].IsAlmostEqualTo(ductAirTerminalPoint2[0]))
						{
							Duct duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
							this.DuctSetParameter(validConnector, duct4, false);
							document.Regenerate();
							this.ConnNewDuctAndConnector(validConnector, duct4);
							Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
							this.DuctSetParameter(validConnector, duct5, false);
							Duct duct6 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint2[1], validConnector2.Origin);
							this.DuctSetParameter(validConnector2, duct6, false);
							document.Regenerate();
							this.ConnNewDuctAndConnector(validConnector2, duct6);
							Duct duct7 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint2[0], ductAirTerminalPoint2[1]);
							this.DuctSetParameter(validConnector2, duct7, false);
							document.Regenerate();
							YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct4);
							YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(duct5);
							YJKPiping<Duct> hypiping7 = new YJKPiping<Duct>(duct6);
							YJKPiping<Duct> hypiping8 = new YJKPiping<Duct>(duct7);
							if (!hypiping.IsYAxis())
							{
								double xaxisAngle2 = hypiping.GetXAxisAngle();
								hypiping5.Rotate(Math.PI*.5 - xaxisAngle2);
								hypiping7.Rotate(Math.PI*.5 - xaxisAngle2);
							}
							if (hypiping6.IsXAxis() && hypiping6.IsRect())
							{
								hypiping6.Rotate90();
							}
							if (hypiping8.IsXAxis() && hypiping8.IsRect())
							{
								hypiping8.Rotate90();
							}
							document.Create.NewElbowFitting(hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[1]));
							document.Create.NewElbowFitting(hypiping7.GetAppropriateConnector(ductAirTerminalPoint2[1]), hypiping8.GetAppropriateConnector(ductAirTerminalPoint2[1]));
							if (num != 1 || num2 != 1)
							{
								connMSG = "四通连接失败，可能原因是风管不相交";
								return false;
							}
							IList<Duct> list4 = hypiping.Separate(ductAirTerminalPoint[0]);
							document.Regenerate();
							if (list4 != null)
							{
								YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(list4[1]);
								document.Create.NewCrossFitting(hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping9.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping8.GetAppropriateConnector(ductAirTerminalPoint[0]));
								flag = true;
							}
							else
							{
								flag = false;
							}
						}
					}
					result = flag;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public IList<Duct> DuctConnectAirTerminalCross(ref Duct duct, FamilyInstance airTerminal, FamilyInstance airTerminal2, ref XYZ endPoint, int terminalConnType, XYZ crossDirection = null)
		{
			IList<Duct> list = new List<Duct>();
			SubTransaction subTransaction = null;
			IList<Duct> result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				Connector validConnector2 = this.GetValidConnector(airTerminal2);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list2 = new List<XYZ>();
				IList<XYZ> list3 = new List<XYZ>();
				list2.Add(validConnector.Origin);
				list2.Add(this.GetConnectorDirectionPoint(validConnector));
				list3.Add(validConnector2.Origin);
				list3.Add(this.GetConnectorDirectionPoint(validConnector2));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list2[0], list2[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				Curve curve3 = YJKLineEx.YJKGetBound(list3[0], list3[1]);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				curve3.MakeUnbound();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				XYZ curveIntersection2 = this.GetCurveIntersection(curve3, curve2);
				if (curveIntersection != null && curveIntersection.IsAlmostEqualTo(curveIntersection2))
				{
					Duct duct2;
					if (terminalConnType == 0)
					{
						duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
						document.Regenerate();
						this.DuctSetParameter(validConnector, duct2, false);
						this.ConnNewDuctAndConnector(validConnector, duct2);
					}
					else
					{
						duct2 = this.FlexDuctConnection(document, curveIntersection, validConnector, hypiping);
						document.Regenerate();
						this.DuctSetParameter(validConnector, duct2, false);
					}
					Duct duct3;
					if (terminalConnType == 0)
					{
						duct3 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector2.Origin);
						document.Regenerate();
						this.DuctSetParameter(validConnector2, duct3, false);
						this.ConnNewDuctAndConnector(validConnector2, duct3);
					}
					else
					{
						duct3 = this.FlexDuctConnection(document, curveIntersection, validConnector2, hypiping);
						document.Regenerate();
						this.DuctSetParameter(validConnector2, duct3, false);
					}
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
					IList<Duct> list4 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (list4 != null)
					{
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(list4[1]);
						List<Connector> list5 = this.OrderCrossConnector(new List<Connector>
						{
							hypiping.GetAppropriateConnector(curveIntersection),
							hypiping4.GetAppropriateConnector(curveIntersection),
							hypiping2.GetAppropriateConnector(curveIntersection),
							hypiping3.GetAppropriateConnector(curveIntersection)
						}, crossDirection);
						document.Create.NewCrossFitting(list5[0], list5[1], list5[2], list5[3]);
						document.Regenerate();
						list.Add(hypiping2.Piping);
						list.Add(hypiping3.Piping);
						duct = this.CheckDuctEndPoint(hypiping, hypiping4, ref endPoint);
					}
					else
					{
						list = null;
					}
				}
				else if (curveIntersection == null && curveIntersection2 == null)
				{
					int num = 1;
					int num2 = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list2[0], list2[1], hypiping, ref num);
					IList<XYZ> ductAirTerminalPoint2 = this.GetDuctAirTerminalPoint(list3[0], list3[1], hypiping, ref num2);
					if (ductAirTerminalPoint != null && ductAirTerminalPoint.Count > 1 && ductAirTerminalPoint2 != null && ductAirTerminalPoint2.Count > 1 && ductAirTerminalPoint[0].IsAlmostEqualTo(ductAirTerminalPoint2[0]))
					{
						Duct duct4;
						if (terminalConnType == 0)
						{
							duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
							this.DuctSetParameter(validConnector, duct4, false);
							document.Regenerate();
							this.ConnNewDuctAndConnector(validConnector, duct4);
						}
						else
						{
							duct4 = this.FlexDuctConnection(document, ductAirTerminalPoint[1], validConnector, hypiping);
							this.DuctSetParameter(validConnector, duct4, false);
							document.Regenerate();
						}
						Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct5, false);
						Duct duct6;
						if (terminalConnType == 0)
						{
							duct6 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint2[1], validConnector2.Origin);
							this.DuctSetParameter(validConnector2, duct6, false);
							document.Regenerate();
							this.ConnNewDuctAndConnector(validConnector2, duct6);
						}
						else
						{
							duct6 = this.FlexDuctConnection(document, ductAirTerminalPoint2[1], validConnector2, hypiping);
							this.DuctSetParameter(validConnector2, duct6, false);
							document.Regenerate();
						}
						Duct duct7 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint2[0], ductAirTerminalPoint2[1]);
						this.DuctSetParameter(validConnector2, duct7, false);
						document.Regenerate();
						YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct4);
						YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(duct5);
						YJKPiping<Duct> hypiping7 = new YJKPiping<Duct>(duct6);
						YJKPiping<Duct> hypiping8 = new YJKPiping<Duct>(duct7);
						if (hypiping6.IsXAxis() && hypiping6.IsRect())
						{
							hypiping6.Rotate90();
						}
						if (hypiping8.IsXAxis() && hypiping8.IsRect())
						{
							hypiping8.Rotate90();
						}
						document.Create.NewElbowFitting(hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[1]));
						document.Create.NewElbowFitting(hypiping7.GetAppropriateConnector(ductAirTerminalPoint2[1]), hypiping8.GetAppropriateConnector(ductAirTerminalPoint2[1]));
						if (num == 1 && num2 == 1)
						{
							IList<Duct> list6 = hypiping.Separate(ductAirTerminalPoint[0]);
							document.Regenerate();
							if (list6 != null)
							{
								YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(list6[1]);
								List<Connector> list7 = this.OrderCrossConnector(new List<Connector>
								{
									hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]),
									hypiping9.GetAppropriateConnector(ductAirTerminalPoint[0]),
									hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]),
									hypiping8.GetAppropriateConnector(ductAirTerminalPoint[0])
								}, crossDirection);
								document.Create.NewCrossFitting(list7[0], list7[1], list7[2], list7[3]);
								list.Add(hypiping6.Piping);
								list.Add(hypiping8.Piping);
								duct = this.CheckDuctEndPoint(hypiping, hypiping9, ref endPoint);
							}
							else
							{
								list = null;
							}
						}
						else
						{
							document.Create.NewTeeFitting(hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping8.GetAppropriateConnector(ductAirTerminalPoint[0]), hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]));
							list.Add(hypiping6.Piping);
							list.Add(hypiping8.Piping);
							duct = this.CheckDuctEndPoint(hypiping, ref endPoint);
						}
					}
				}
				subTransaction.Commit();
				result = list;
			}
			catch (Exception)
			{
				list = null;
				result = list;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public IList<Duct> DuctCrossConnTerminal(ref Duct duct, FamilyInstance airTerminal, FamilyInstance airTerminal2, ref XYZ endPoint, int terminalConnType, XYZ globePnt, int alignType, XYZ crossDirection = null)
		{
			IList<Duct> list = new List<Duct>();
			SubTransaction subTransaction = null;
			IList<Duct> result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				Connector validConnector2 = this.GetValidConnector(airTerminal2);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list2 = new List<XYZ>();
				IList<XYZ> list3 = new List<XYZ>();
				list2.Add(validConnector.Origin);
				list2.Add(this.GetConnectorDirectionPoint(validConnector));
				list3.Add(validConnector2.Origin);
				list3.Add(this.GetConnectorDirectionPoint(validConnector2));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list2[0], list2[1]);
				curve.MakeUnbound();
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				curve2.MakeUnbound();
				Curve curve3 = YJKLineEx.YJKGetBound(list3[0], list3[1]);
				curve3.MakeUnbound();
				IList<Duct> list4 = new List<Duct>();
				IList<XYZ> list5 = new List<XYZ>();
				string text = "";
				List<string> list6 = new List<string>();
				bool curveIntersection = this.GetCurveIntersection(curve, curve2) != null;
				XYZ curveIntersection2 = this.GetCurveIntersection(curve3, curve2);
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				ConnectDuctsByFitting connectDuctsByFitting = new ConnectDuctsByFitting();
				string connectorSharpName = YJKRevitTools.GetConnectorSharpName(hypiping.StartConnector.Connector);
				if (!curveIntersection && curveIntersection2 == null)
				{
					int num = 1;
					int num2 = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list2[0], list2[1], hypiping, ref num);
					IList<XYZ> ductAirTerminalPoint2 = this.GetDuctAirTerminalPoint(list3[0], list3[1], hypiping, ref num2);
					if (ductAirTerminalPoint == null || ductAirTerminalPoint.Count < 2 || ductAirTerminalPoint2 == null || ductAirTerminalPoint2.Count < 2)
					{
						return null;
					}
					if (!ductAirTerminalPoint[0].IsAlmostEqualTo(ductAirTerminalPoint2[0], 0.0001))
					{
						return null;
					}
					Duct piping = null;
					Duct piping2 = null;
					this.CreatDuctsTerminalToDuct(ref piping, ref piping2, hypiping, validConnector, ductAirTerminalPoint, terminalConnType, alignType);
					Duct piping3 = null;
					Duct piping4 = null;
					this.CreatDuctsTerminalToDuct(ref piping3, ref piping4, hypiping, validConnector2, ductAirTerminalPoint2, terminalConnType, alignType);
					YJKPiping<Duct> hyDuct = new YJKPiping<Duct>(piping);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(piping2);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(piping3);
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(piping4);
					this.RotateNewCreatRiserDuct(hypiping, hyDuct, hypiping3, hypiping2, hypiping4, alignType);
					this.ConnElbowDucts(hyDuct, hypiping2, ductAirTerminalPoint[1], document);
					this.ConnElbowDucts(hypiping3, hypiping4, ductAirTerminalPoint2[1], document);
					if (num == 1 && num2 == 1)
					{
						IList<Duct> list7 = hypiping.Separate(ductAirTerminalPoint[0]);
						document.Regenerate();
						if (list7 == null)
						{
							list = null;
						}
						else
						{
							hyrotateFamilyInstance.MovePiping(list7[0]);
							hyrotateFamilyInstance.MovePiping(list7[1]);
							YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(list7[1]);
							this.OrderCrossConnector(new List<Connector>
							{
								hypiping.GetNearConnectorToPoint(ductAirTerminalPoint[0]),
								hypiping5.GetNearConnectorToPoint(ductAirTerminalPoint[0]),
								hypiping2.GetNearConnectorToPoint(ductAirTerminalPoint[0]),
								hypiping4.GetNearConnectorToPoint(ductAirTerminalPoint[0])
							}, crossDirection);
							list4 = new List<Duct>();
							list5 = new List<XYZ>();
							if (this.CompareDuctDistance(list7[0], list7[1], globePnt))
							{
								list4.Add(list7[0]);
								hypiping = new YJKPiping<Duct>(list7[0]);
								list5.Add(YJKRevitTools.GetElementCurve(list7[0], 0) + YJKRevitTools.GetElementCurve(list7[0], 1) / 2.0);
								list4.Add(list7[1]);
								hypiping5 = new YJKPiping<Duct>(list7[1]);
								list5.Add(YJKRevitTools.GetElementCurve(list7[1], 0) + YJKRevitTools.GetElementCurve(list7[1], 1) / 2.0);
							}
							else
							{
								list4.Add(list7[1]);
								hypiping = new YJKPiping<Duct>(list7[1]);
								list5.Add(YJKRevitTools.GetElementCurve(list7[1], 0) + YJKRevitTools.GetElementCurve(list7[1], 1) / 2.0);
								list4.Add(list7[0]);
								hypiping5 = new YJKPiping<Duct>(list7[0]);
								list5.Add(YJKRevitTools.GetElementCurve(list7[0], 0) + YJKRevitTools.GetElementCurve(list7[0], 1) / 2.0);
							}
							list4.Add(hypiping2.Piping);
							list5.Add(YJKRevitTools.GetElementCurve(hypiping2.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping2.Piping, 1) / 2.0);
							list4.Add(hypiping4.Piping);
							list5.Add(YJKRevitTools.GetElementCurve(hypiping4.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping4.Piping, 1) / 2.0);
							list6 = new List<string>();
							if (connectorSharpName == "矩形")
							{
								list6 = new ConnectDuctManage().GetRoundAngleCrossNames();
								connectDuctsByFitting.IsConnecteddByCross(list4, list5, list6, ref text);
							}
							else
							{
								if (!(connectorSharpName == "圆形"))
								{
									throw new Exception("目前不支持椭圆形风管及风口连接！");
								}
								list6.Add("圆形插管四通-顶对齐");
								list6.Add("圆形插管四通-中心对齐");
								list6.Add("圆形插管四通-底对齐");
								this.NewCrossFitting(list4, list5, list6, ref text);
							}
							list.Add(hypiping2.Piping);
							list.Add(hypiping4.Piping);
							hyrotateFamilyInstance.MovePiping(hypiping2.Piping);
							hyrotateFamilyInstance.MovePiping(hypiping4.Piping);
							duct = this.CheckDuctEndPoint(hypiping, hypiping5, ref endPoint);
							hyrotateFamilyInstance.MovePiping(duct);
						}
					}
					else
					{
						list6 = new List<string>();
						if (connectorSharpName == "矩形")
						{
							list4 = new List<Duct>();
							list5 = new List<XYZ>();
							list4.Add(hypiping.Piping);
							list5.Add(YJKRevitTools.GetElementCurve(hypiping.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping.Piping, 1) / 2.0);
							list4.Add(hypiping2.Piping);
							list5.Add(YJKRevitTools.GetElementCurve(hypiping2.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping2.Piping, 1) / 2.0);
							list4.Add(hypiping4.Piping);
							list5.Add(YJKRevitTools.GetElementCurve(hypiping4.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping4.Piping, 1) / 2.0);
							list6 = new ConnectDuctManage().GetYShapeTeeNames();
							connectDuctsByFitting.IsConnecteddByYShapeTee(list4, list5, list6, ref text);
						}
						else
						{
							if (!(connectorSharpName == "圆形"))
							{
								throw new Exception("目前不支持椭圆形风管及风口连接！");
							}
							list4 = new List<Duct>();
							list5 = new List<XYZ>();
							list4.Add(hypiping2.Piping);
							list5.Add(YJKRevitTools.GetElementCurve(hypiping2.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping2.Piping, 1) / 2.0);
							list4.Add(hypiping4.Piping);
							list5.Add(YJKRevitTools.GetElementCurve(hypiping4.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping4.Piping, 1) / 2.0);
							list4.Add(hypiping.Piping);
							list5.Add(YJKRevitTools.GetElementCurve(hypiping.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping.Piping, 1) / 2.0);
							list6.Add("圆形斜角三通-顶对齐");
							list6.Add("圆形斜角三通-中心对齐");
							list6.Add("圆形斜角三通-底对齐");
							this.NewTeeFittingT(list4, list5, list6, ref text);
						}
						list.Add(hypiping2.Piping);
						list.Add(hypiping4.Piping);
						duct = this.CheckDuctEndPoint(hypiping, ref endPoint);
						hyrotateFamilyInstance.MovePiping(duct);
					}
				}
				subTransaction.Commit();
				result = list;
			}
			catch (Exception)
			{
				list = null;
				result = list;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private string ConnCrossDucts(YJKPiping<Duct> hyDuct1, YJKPiping<Duct> hyDuct2, YJKPiping<Duct> hyDuct3, YJKPiping<Duct> hyDuct4, string shapeName)
		{
			string result = "";
			List<string> list = new List<string>();
			IList<Duct> list2 = new List<Duct>();
			IList<XYZ> list3 = new List<XYZ>();
			list2.Add(hyDuct1.Piping);
			list3.Add(this.GetDuctMiddlePoint(hyDuct1));
			list2.Add(hyDuct2.Piping);
			list3.Add(this.GetDuctMiddlePoint(hyDuct2));
			list2.Add(hyDuct3.Piping);
			list3.Add(this.GetDuctMiddlePoint(hyDuct3));
			list2.Add(hyDuct4.Piping);
			list3.Add(this.GetDuctMiddlePoint(hyDuct4));
			if (shapeName == "矩形")
			{
				list = new ConnectDuctManage().GetRoundAngleCrossNames();
				new ConnectDuctsByFitting().IsConnecteddByCross(list2, list3, list, ref result);
			}
			else if (shapeName == "圆形")
			{
				list.Add("圆形插管四通-顶对齐");
				list.Add("圆形插管四通-中心对齐");
				list.Add("圆形插管四通-底对齐");
				this.NewCrossFitting(list2, list3, list, ref result);
			}
			else
			{
				result = "目前不支持椭圆形风管及风口连接！";
			}
			return result;
		}

		private XYZ GetDuctMiddlePoint(YJKPiping<Duct> hyDuct)
		{
			XYZ origin = hyDuct.StartConnector.Origin;
			XYZ origin2 = hyDuct.EndConnector.Origin;
			return origin.Add(origin2) / 2.0;
		}

		private void CreatDuctsTerminalToDuct(ref Duct nearTerminalDuct, ref Duct turnDuct, YJKPiping<Duct> myDuct, Connector conn, IList<XYZ> iPoints, int terminalConnType, int alignType)
		{
            Autodesk.Revit.DB.Document document = myDuct.Piping.Document;
			if (terminalConnType == 0)
			{
				nearTerminalDuct = YJKRevitTools.NewDuct(myDuct.Piping, iPoints[1], conn.Origin);
				this.DuctSetParameter(conn, nearTerminalDuct, false);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(nearTerminalDuct);
				Connector closeConnector = hypiping.GetCloseConnector(conn.Origin, false);
				if (!this.IsConnectSameDirection(conn, closeConnector))
				{
					hypiping.Rotate(Math.PI*.5);
				}
				closeConnector.ConnectTo(conn);
			}
			else
			{
				nearTerminalDuct = this.FlexDuctConnection(document, iPoints[1], conn, myDuct);
			}
			turnDuct = YJKRevitTools.NewDuct(myDuct.Piping, iPoints[0], iPoints[1]);
			this.DuctSetParameter(conn, turnDuct, false);
			document.Regenerate();
			this.AlignDucts(myDuct.Piping, turnDuct, alignType);
		}

		private void AlignDucts(Duct baseDuct, Duct changeDuct, int alignType)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(baseDuct);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(changeDuct);
			double heightOrDiameter = hypiping.GetHeightOrDiameter();
			double offSet = hypiping.GetOffSet();
			double heightOrDiameter2 = hypiping2.GetHeightOrDiameter();
			double offSet2 = hypiping2.GetOffSet();
			XYZ xyz = new XYZ(0.0, 0.0, 1.0);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			if (alignType == 0)
			{
				double num = offSet + heightOrDiameter / 2.0 - heightOrDiameter2 / 2.0 - offSet2;
				hyrotateFamilyInstance.MoveElement(num * xyz, changeDuct);
				baseDuct.Document.Regenerate();
			}
			if (alignType == 2)
			{
				double num2 = offSet - heightOrDiameter / 2.0 + heightOrDiameter2 / 2.0 - offSet2;
				hyrotateFamilyInstance.MoveElement(num2 * xyz, changeDuct);
				baseDuct.Document.Regenerate();
			}
		}

		private void RotateNewCreatRiserDuct(YJKPiping<Duct> myDuct, YJKPiping<Duct> hyDuct1, YJKPiping<Duct> hyDuct12, YJKPiping<Duct> hyDuct2, YJKPiping<Duct> hyDuct22, int alignType)
		{
			if (!myDuct.IsYAxis())
			{
				double xaxisAngle = myDuct.GetXAxisAngle();
				hyDuct1.Rotate(Math.PI - xaxisAngle);
				hyDuct12.Rotate(Math.PI - xaxisAngle);
			}
		}

		private void ConnElbowDucts(YJKPiping<Duct> hyDuct1, YJKPiping<Duct> hyDuct2, XYZ point, Autodesk.Revit.DB.Document doc)
		{
			string empty = string.Empty;
			FamilySymbol familySymbol = doc.GetElementById(hyDuct1.Piping.DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)1, 0).MEPPartId) as FamilySymbol;
			if (familySymbol == null)
			{
				if (!this.IsExistElbowFitting(doc, out familySymbol))
				{
					SubTransaction subTransaction = null;
					try
					{
						subTransaction = new SubTransaction(doc);
						subTransaction.Start();
						familySymbol = new ConnectDuctManage().GetFamilySymbol("内外弧形矩形弯头", doc, ref empty);
						if (familySymbol == null)
						{
							throw new Exception("弯头族不存在或者加载地址不正确！");
						}
						UnifiedModified.ActivateFamilySymbol(familySymbol);
						doc.Regenerate();
						subTransaction.Commit();
					}
					catch (Exception ex)
					{
						if ((int)subTransaction.GetStatus() != 3 && (int)subTransaction.GetStatus() == 1)
						{
							subTransaction.RollBack();
						}
						throw ex;
					}
				}
				if (!hyDuct1.ChangeFirstElbow(familySymbol))
				{
					throw new Exception("设置风管弯头，\n首选连接件出错！");
				}
			}
			if (!(doc.GetElementById(hyDuct2.Piping.DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)1, 0).MEPPartId) is FamilySymbol))
			{
				FamilySymbol fs = familySymbol;
				if (!hyDuct2.ChangeFirstElbow(fs))
				{
					throw new Exception("设置风管弯头，\n首选连接件出错！");
				}
			}
			if (Math.Abs(hyDuct1.GetWeightOrDiameter() - hyDuct2.GetWeightOrDiameter()) > 0.0001 || Math.Abs(hyDuct1.GetHeightOrDiameter() - hyDuct2.GetHeightOrDiameter()) > 0.0001)
			{
				FamilySymbol familySymbol2 = doc.GetElementById(hyDuct1.Piping.DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)4, 0).MEPPartId) as FamilySymbol;
				if (familySymbol2 == null)
				{
					familySymbol2 = new ConnectDuctManage().GetFamilySymbol("矩形变径", doc, ref empty);
					if (familySymbol2 == null)
					{
						throw new Exception("变径族不存在或者加载地址不正确！");
					}
				}
				hyDuct1.ChangeFirstTransition(familySymbol2);
				hyDuct2.ChangeFirstTransition(familySymbol2);
			}
			Connector nearConnectorToPoint = hyDuct1.GetNearConnectorToPoint(point);
			Connector nearConnectorToPoint2 = hyDuct2.GetNearConnectorToPoint(point);
			doc.Create.NewElbowFitting(nearConnectorToPoint, nearConnectorToPoint2);
		}

		public IList<Duct> DuctConnectAirTerminalCross(ref Duct duct, FamilyInstance airTerminal, FamilyInstance airTerminal2, ref XYZ endPoint, int terminalConnType, XYZ globePnt, XYZ crossDirection = null)
		{
			IList<Duct> list = new List<Duct>();
			SubTransaction subTransaction = null;
			IList<Duct> result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				Connector validConnector = this.GetValidConnector(airTerminal);
				Connector validConnector2 = this.GetValidConnector(airTerminal2);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<XYZ> list2 = new List<XYZ>();
				IList<XYZ> list3 = new List<XYZ>();
				list2.Add(validConnector.Origin);
				list2.Add(this.GetConnectorDirectionPoint(validConnector));
				list3.Add(validConnector2.Origin);
				list3.Add(this.GetConnectorDirectionPoint(validConnector2));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				Curve curve = YJKLineEx.YJKGetBound(list2[0], list2[1]);
				Curve curve2 = YJKLineEx.YJKGetBound(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				Curve curve3 = YJKLineEx.YJKGetBound(list3[0], list3[1]);
				curve.MakeUnbound();
				curve2.MakeUnbound();
				curve3.MakeUnbound();
				IList<Duct> list4 = new List<Duct>();
				IList<XYZ> list5 = new List<XYZ>();
				string text = "";
				List<string> list6 = new List<string>();
				XYZ curveIntersection = this.GetCurveIntersection(curve, curve2);
				XYZ curveIntersection2 = this.GetCurveIntersection(curve3, curve2);
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				ConnectDuctsByFitting connectDuctsByFitting = new ConnectDuctsByFitting();
				string connectorSharpName = YJKRevitTools.GetConnectorSharpName(hypiping.StartConnector.Connector);
				if (curveIntersection != null && curveIntersection.IsAlmostEqualTo(curveIntersection2))
				{
					Duct duct2;
					if (terminalConnType == 0)
					{
						duct2 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector.Origin);
					}
					else
					{
						duct2 = this.FlexDuctConnection(document, curveIntersection, validConnector, hypiping);
					}
					document.Regenerate();
					this.DuctSetParameter(validConnector, duct2, false);
					Duct duct3;
					if (terminalConnType == 0)
					{
						duct3 = YJKRevitTools.NewDuct(hypiping.Piping, curveIntersection, validConnector2.Origin);
					}
					else
					{
						duct3 = this.FlexDuctConnection(document, curveIntersection, validConnector2, hypiping);
					}
					document.Regenerate();
					this.DuctSetParameter(validConnector2, duct3, false);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
					hypiping2.GetCloseConnector(validConnector.Origin, false).ConnectTo(validConnector);
					hypiping3.GetCloseConnector(validConnector2.Origin, false).ConnectTo(validConnector2);
					IList<Duct> list7 = hypiping.Separate(curveIntersection);
					document.Regenerate();
					if (!hypiping.IsYAxis())
					{
						double xaxisAngle = hypiping.GetXAxisAngle();
						hypiping2.Rotate(Math.PI - xaxisAngle);
						hypiping3.Rotate(Math.PI - xaxisAngle);
					}
					if (list7 != null)
					{
						hyrotateFamilyInstance.MovePiping(list7[0]);
						hyrotateFamilyInstance.MovePiping(list7[1]);
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(list7[1]);
						this.OrderCrossConnector(new List<Connector>
						{
							hypiping.GetAppropriateConnector(curveIntersection),
							hypiping4.GetAppropriateConnector(curveIntersection),
							hypiping2.GetAppropriateConnector(curveIntersection),
							hypiping3.GetAppropriateConnector(curveIntersection)
						}, crossDirection);
						list4 = new List<Duct>();
						list5 = new List<XYZ>();
						if (this.CompareDuctDistance(list7[0], list7[1], globePnt))
						{
							list4.Add(list7[0]);
							hypiping = new YJKPiping<Duct>(list7[0]);
							list5.Add(YJKRevitTools.GetElementCurve(list7[0], 0) + YJKRevitTools.GetElementCurve(list7[0], 1) / 2.0);
							list4.Add(list7[1]);
							hypiping4 = new YJKPiping<Duct>(list7[1]);
							list5.Add(YJKRevitTools.GetElementCurve(list7[1], 0) + YJKRevitTools.GetElementCurve(list7[1], 1) / 2.0);
						}
						else
						{
							list4.Add(list7[1]);
							hypiping = new YJKPiping<Duct>(list7[1]);
							list5.Add(YJKRevitTools.GetElementCurve(list7[1], 0) + YJKRevitTools.GetElementCurve(list7[1], 1) / 2.0);
							list4.Add(list7[0]);
							hypiping4 = new YJKPiping<Duct>(list7[0]);
							list5.Add(YJKRevitTools.GetElementCurve(list7[0], 0) + YJKRevitTools.GetElementCurve(list7[0], 1) / 2.0);
						}
						list4.Add(duct2);
						list5.Add(YJKRevitTools.GetElementCurve(duct2, 0) + YJKRevitTools.GetElementCurve(duct2, 1) / 2.0);
						list4.Add(duct3);
						list5.Add(YJKRevitTools.GetElementCurve(duct3, 0) + YJKRevitTools.GetElementCurve(duct3, 1) / 2.0);
						list6 = new List<string>();
						if (connectorSharpName == "矩形")
						{
							list6 = new ConnectDuctManage().GetRoundAngleCrossNames();
							connectDuctsByFitting.IsConnecteddByCross(list4, list5, list6, ref text);
						}
						else
						{
							if (!(connectorSharpName == "圆形"))
							{
								throw new Exception("目前不支持椭圆形风管及风口连接！");
							}
							list6.Add("圆形插管四通-顶对齐");
							list6.Add("圆形插管四通-中心对齐");
							list6.Add("圆形插管四通-底对齐");
							this.NewCrossFitting(list4, list5, list6, ref text);
						}
						document.Regenerate();
						list.Add(hypiping2.Piping);
						list.Add(hypiping3.Piping);
						hyrotateFamilyInstance.MovePiping(hypiping2.Piping);
						hyrotateFamilyInstance.MovePiping(hypiping3.Piping);
						duct = this.CheckDuctEndPoint(hypiping, hypiping4, ref endPoint);
						hyrotateFamilyInstance.MovePiping(duct);
					}
					else
					{
						list = null;
					}
				}
				else if (curveIntersection == null && curveIntersection2 == null)
				{
					int num = 1;
					int num2 = 1;
					IList<XYZ> ductAirTerminalPoint = this.GetDuctAirTerminalPoint(list2[0], list2[1], hypiping, ref num);
					IList<XYZ> ductAirTerminalPoint2 = this.GetDuctAirTerminalPoint(list3[0], list3[1], hypiping, ref num2);
					if (ductAirTerminalPoint != null && ductAirTerminalPoint.Count > 1 && ductAirTerminalPoint2 != null && ductAirTerminalPoint2.Count > 1 && ductAirTerminalPoint[0].IsAlmostEqualTo(ductAirTerminalPoint2[0]))
					{
						Duct duct4;
						if (terminalConnType == 0)
						{
							duct4 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[1], validConnector.Origin);
						}
						else
						{
							duct4 = this.FlexDuctConnection(document, ductAirTerminalPoint[1], validConnector, hypiping);
						}
						this.DuctSetParameter(validConnector, duct4, false);
						Duct duct5 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint[0], ductAirTerminalPoint[1]);
						this.DuctSetParameter(validConnector, duct5, false);
						Duct duct6;
						if (terminalConnType == 0)
						{
							duct6 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint2[1], validConnector2.Origin);
						}
						else
						{
							duct6 = this.FlexDuctConnection(document, ductAirTerminalPoint2[1], validConnector2, hypiping);
						}
						this.DuctSetParameter(validConnector2, duct6, false);
						Duct duct7 = YJKRevitTools.NewDuct(hypiping.Piping, ductAirTerminalPoint2[0], ductAirTerminalPoint2[1]);
						this.DuctSetParameter(validConnector2, duct7, false);
						document.Regenerate();
						YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct4);
						YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(duct5);
						YJKPiping<Duct> hypiping7 = new YJKPiping<Duct>(duct6);
						YJKPiping<Duct> hypiping8 = new YJKPiping<Duct>(duct7);
						hypiping5.GetCloseConnector(validConnector.Origin, false).ConnectTo(validConnector);
						hypiping7.GetCloseConnector(validConnector2.Origin, false).ConnectTo(validConnector2);
						if (!hypiping.IsYAxis())
						{
							double xaxisAngle2 = hypiping.GetXAxisAngle();
							hypiping5.Rotate(Math.PI - xaxisAngle2);
							hypiping7.Rotate(Math.PI - xaxisAngle2);
						}
						if (hypiping6.IsXAxis() && hypiping6.IsRect())
						{
							hypiping6.Rotate90();
						}
						if (hypiping8.IsXAxis() && hypiping8.IsRect())
						{
							hypiping8.Rotate90();
						}
						document.Create.NewElbowFitting(hypiping5.GetAppropriateConnector(ductAirTerminalPoint[1]), hypiping6.GetAppropriateConnector(ductAirTerminalPoint[1]));
						document.Create.NewElbowFitting(hypiping7.GetAppropriateConnector(ductAirTerminalPoint2[1]), hypiping8.GetAppropriateConnector(ductAirTerminalPoint2[1]));
						if (num == 1 && num2 == 1)
						{
							IList<Duct> list8 = hypiping.Separate(ductAirTerminalPoint[0]);
							document.Regenerate();
							if (list8 != null)
							{
								hyrotateFamilyInstance.MovePiping(list8[0]);
								hyrotateFamilyInstance.MovePiping(list8[1]);
								YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(list8[1]);
								this.OrderCrossConnector(new List<Connector>
								{
									hypiping.GetAppropriateConnector(ductAirTerminalPoint[0]),
									hypiping9.GetAppropriateConnector(ductAirTerminalPoint[0]),
									hypiping6.GetAppropriateConnector(ductAirTerminalPoint[0]),
									hypiping8.GetAppropriateConnector(ductAirTerminalPoint[0])
								}, crossDirection);
								list4 = new List<Duct>();
								list5 = new List<XYZ>();
								if (this.CompareDuctDistance(list8[0], list8[1], globePnt))
								{
									list4.Add(list8[0]);
									hypiping = new YJKPiping<Duct>(list8[0]);
									list5.Add(YJKRevitTools.GetElementCurve(list8[0], 0) + YJKRevitTools.GetElementCurve(list8[0], 1) / 2.0);
									list4.Add(list8[1]);
									hypiping9 = new YJKPiping<Duct>(list8[1]);
									list5.Add(YJKRevitTools.GetElementCurve(list8[1], 0) + YJKRevitTools.GetElementCurve(list8[1], 1) / 2.0);
								}
								else
								{
									list4.Add(list8[1]);
									hypiping = new YJKPiping<Duct>(list8[1]);
									list5.Add(YJKRevitTools.GetElementCurve(list8[1], 0) + YJKRevitTools.GetElementCurve(list8[1], 1) / 2.0);
									list4.Add(list8[0]);
									hypiping9 = new YJKPiping<Duct>(list8[0]);
									list5.Add(YJKRevitTools.GetElementCurve(list8[0], 0) + YJKRevitTools.GetElementCurve(list8[0], 1) / 2.0);
								}
								list4.Add(hypiping6.Piping);
								list5.Add(YJKRevitTools.GetElementCurve(hypiping6.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping6.Piping, 1) / 2.0);
								list4.Add(hypiping8.Piping);
								list5.Add(YJKRevitTools.GetElementCurve(hypiping8.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping8.Piping, 1) / 2.0);
								list6 = new List<string>();
								if (connectorSharpName == "矩形")
								{
									list6 = new ConnectDuctManage().GetRoundAngleCrossNames();
									connectDuctsByFitting.IsConnecteddByCross(list4, list5, list6, ref text);
								}
								else
								{
									if (!(connectorSharpName == "圆形"))
									{
										throw new Exception("目前不支持椭圆形风管及风口连接！");
									}
									list6.Add("圆形插管四通-顶对齐");
									list6.Add("圆形插管四通-中心对齐");
									list6.Add("圆形插管四通-底对齐");
									this.NewCrossFitting(list4, list5, list6, ref text);
								}
								list.Add(hypiping6.Piping);
								list.Add(hypiping8.Piping);
								hyrotateFamilyInstance.MovePiping(hypiping6.Piping);
								hyrotateFamilyInstance.MovePiping(hypiping8.Piping);
								duct = this.CheckDuctEndPoint(hypiping, hypiping9, ref endPoint);
								hyrotateFamilyInstance.MovePiping(duct);
							}
							else
							{
								list = null;
							}
						}
						else
						{
							list6 = new List<string>();
							if (connectorSharpName == "矩形")
							{
								list4 = new List<Duct>();
								list5 = new List<XYZ>();
								list4.Add(hypiping.Piping);
								list5.Add(YJKRevitTools.GetElementCurve(hypiping.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping.Piping, 1) / 2.0);
								list4.Add(hypiping6.Piping);
								list5.Add(YJKRevitTools.GetElementCurve(hypiping6.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping6.Piping, 1) / 2.0);
								list4.Add(hypiping8.Piping);
								list5.Add(YJKRevitTools.GetElementCurve(hypiping8.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping8.Piping, 1) / 2.0);
								list6 = new ConnectDuctManage().GetYShapeTeeNames();
								connectDuctsByFitting.IsConnecteddByYShapeTee(list4, list5, list6, ref text);
							}
							else
							{
								if (!(connectorSharpName == "圆形"))
								{
									throw new Exception("目前不支持椭圆形风管及风口连接！");
								}
								list4 = new List<Duct>();
								list5 = new List<XYZ>();
								list4.Add(hypiping6.Piping);
								list5.Add(YJKRevitTools.GetElementCurve(hypiping6.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping6.Piping, 1) / 2.0);
								list4.Add(hypiping8.Piping);
								list5.Add(YJKRevitTools.GetElementCurve(hypiping8.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping8.Piping, 1) / 2.0);
								list4.Add(hypiping.Piping);
								list5.Add(YJKRevitTools.GetElementCurve(hypiping.Piping, 0) + YJKRevitTools.GetElementCurve(hypiping.Piping, 1) / 2.0);
								list6.Add("圆形斜角三通-顶对齐");
								list6.Add("圆形斜角三通-中心对齐");
								list6.Add("圆形斜角三通-底对齐");
								this.NewTeeFittingT(list4, list5, list6, ref text);
							}
							list.Add(hypiping6.Piping);
							list.Add(hypiping8.Piping);
							duct = this.CheckDuctEndPoint(hypiping, ref endPoint);
							hyrotateFamilyInstance.MovePiping(duct);
						}
					}
				}
				subTransaction.Commit();
				result = list;
			}
			catch (Exception)
			{
				list = null;
				result = list;
			}
			finally
			{
				TransactionStatus status = subTransaction.GetStatus();
				if (subTransaction != null && 3 != (int)status)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private XYZ GetConnectorDirectionPoint(Connector connector)
		{
			XYZ result = null;
			if (connector != null)
			{
				Transform coordinateSystem = connector.CoordinateSystem;
				result = connector.Origin + 0.1 * coordinateSystem.BasisZ;
			}
			return result;
		}

		private void DuctFittingSet(Duct duct)
		{
			bool flag = false;
			bool flag2 = false;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			if (hypiping.CheckFirstTeeIsNull() || hypiping.CheckFirstElbowIsNull() || hypiping.CheckFirstCrossIsNull() || hypiping.CheckFirstTransitionIsNull())
			{
				flag = true;
			}
			DuctFittingSet ductFittingSetText = new HVACUserSetting().GetDuctFittingSetText();
			if (string.IsNullOrEmpty(ductFittingSetText.RectFitting.Elbow) || string.IsNullOrEmpty(ductFittingSetText.RectFitting.Tee) || string.IsNullOrEmpty(ductFittingSetText.RectFitting.Cross) || string.IsNullOrEmpty(ductFittingSetText.RectFitting.TakeOff) || string.IsNullOrEmpty(ductFittingSetText.RectFitting.Transition) || string.IsNullOrEmpty(ductFittingSetText.RectFitting.TYDF) || string.IsNullOrEmpty(ductFittingSetText.RoundFitting.Elbow) || string.IsNullOrEmpty(ductFittingSetText.RoundFitting.Tee) || string.IsNullOrEmpty(ductFittingSetText.RoundFitting.Cross) || string.IsNullOrEmpty(ductFittingSetText.RoundFitting.Transition) || string.IsNullOrEmpty(ductFittingSetText.RoundFitting.TYDF))
			{
				flag2 = true;
			}
			if (flag && flag2)
			{
				throw new Exception("请先配置风管管件。");
			}
			if (!flag2)
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				HVACDataReader hvacdataReader = new HVACDataReader(hypiping.Piping.Document);
				List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(document, typeof(FamilySymbol), BuiltInCategory.OST_DuctFitting);
				if (!hypiping.CheckFirstTee(ductFittingSetText.RectFitting.Tee) && !hypiping.CheckFirstTee(ductFittingSetText.RoundFitting.Tee))
				{
					FamilySymbol familySymbol = this.GetExistFamilySymbos(elementByCategory, hypiping, ductFittingSetText.RectFitting.Tee, ductFittingSetText.RoundFitting.Tee);
					if (familySymbol == null)
					{
						familySymbol = this.LoadFamilyBySection(ref hvacdataReader, hypiping, ductFittingSetText.RectFitting.Tee, ductFittingSetText.RoundFitting.Tee);
					}
					hypiping.ChangeFirstTee(familySymbol);
				}
				if (!hypiping.CheckFirstElbow(ductFittingSetText.RectFitting.Elbow) && !hypiping.CheckFirstElbow(ductFittingSetText.RoundFitting.Elbow))
				{
					FamilySymbol familySymbol = this.GetExistFamilySymbos(elementByCategory, hypiping, ductFittingSetText.RectFitting.Elbow, ductFittingSetText.RoundFitting.Elbow);
					if (familySymbol == null)
					{
						familySymbol = this.LoadFamilyBySection(ref hvacdataReader, hypiping, ductFittingSetText.RectFitting.Elbow, ductFittingSetText.RoundFitting.Elbow);
					}
					hypiping.ChangeFirstElbow(familySymbol);
				}
				if (!hypiping.CheckFirstCross(ductFittingSetText.RectFitting.Cross) && !hypiping.CheckFirstCross(ductFittingSetText.RoundFitting.Cross))
				{
					FamilySymbol familySymbol = this.GetExistFamilySymbos(elementByCategory, hypiping, ductFittingSetText.RectFitting.Cross, ductFittingSetText.RoundFitting.Cross);
					if (familySymbol == null)
					{
						familySymbol = this.LoadFamilyBySection(ref hvacdataReader, hypiping, ductFittingSetText.RectFitting.Cross, ductFittingSetText.RoundFitting.Cross);
					}
					hypiping.ChangeFirstCross(familySymbol);
				}
				if (!hypiping.CheckFirstTransition(ductFittingSetText.RectFitting.Transition) && !hypiping.CheckFirstTransition(ductFittingSetText.RoundFitting.Transition))
				{
					FamilySymbol familySymbol = this.GetExistFamilySymbos(elementByCategory, hypiping, ductFittingSetText.RectFitting.Transition, ductFittingSetText.RoundFitting.Transition);
					if (familySymbol == null)
					{
						familySymbol = this.LoadFamilyBySection(ref hvacdataReader, hypiping, ductFittingSetText.RectFitting.Transition, ductFittingSetText.RoundFitting.Transition);
					}
					hypiping.ChangeFirstTransition(familySymbol);
				}
			}
		}

		private FamilySymbol GetExistFamilySymbos(List<Element> fsList, YJKPiping<Duct> hyDuct, string fsNameRect, string fsNameRound)
		{
			FamilySymbol result = null;
			foreach (Element element in fsList)
			{
				if (hyDuct.StartConnector.Connector.Shape == ConnectorSharp.Rect)
				{
					if (element.Name == fsNameRect)
					{
						result = (element as FamilySymbol);
					}
				}
				else if (element.Name == fsNameRound)
				{
					result = (element as FamilySymbol);
				}
			}
			return result;
		}

		private FamilySymbol LoadFamilyBySection(ref HVACDataReader hvacDr, YJKPiping<Duct> hyDuct, string fsNameRect, string fsNameRound)
		{
			FamilySymbol result;
			if (hyDuct.StartConnector.Connector.Shape == ConnectorSharp.Rect)
			{
				DbFamily familyByName = hvacDr.GetFamilyByName(fsNameRect);
				result = hvacDr.FamilyLoad(familyByName, hyDuct.Piping.Document);
			}
			else
			{
				DbFamily familyByName = hvacDr.GetFamilyByName(fsNameRound);
				result = hvacDr.FamilyLoad(familyByName, hyDuct.Piping.Document);
			}
			return result;
		}

		private Duct CheckDuctLength(Duct duct, IList<Element> listTermianl)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			List<XYZ> list3 = new List<XYZ>();
			foreach (Element elem in listTermianl)
			{
				XYZ loctaionPoint = YJKRevitTools.GetLoctaionPoint(elem);
				list.Add(hypiping.GetProject(loctaionPoint));
				list2.Add(hypiping.GetLineProject(loctaionPoint));
			}
			bool flag = false;
			foreach (XYZ xyz in list2)
			{
				flag = false;
				using (List<XYZ>.Enumerator enumerator3 = list.GetEnumerator())
				{
					while (enumerator3.MoveNext())
					{
						if (enumerator3.Current.IsAlmostEqualTo(xyz))
						{
							flag = true;
						}
					}
				}
				if (!flag)
				{
					list3.Add(xyz);
				}
			}
			Duct result;
			if (list3.Count > 0)
			{
				XYZ xyz2 = hypiping.StartConnector.Origin;
				XYZ xyz3 = hypiping.EndConnector.Origin;
				XYZ xyz4 = (xyz3 - xyz2).Normalize();
				foreach (XYZ xyz5 in list3)
				{
					if (xyz2.DistanceTo(xyz5) < xyz3.DistanceTo(xyz5))
					{
						if ((xyz2 - xyz5).Normalize().IsAlmostEqualTo(xyz4))
						{
							xyz2 = xyz5;
						}
					}
					else if ((xyz5 - xyz3).Normalize().IsAlmostEqualTo(xyz4))
					{
						xyz3 = xyz5;
					}
				}
				result = hypiping.Extend(xyz2, xyz3);
			}
			else
			{
				result = duct;
			}
			return result;
		}

		public bool DuctConnectAirTerminalByRectangle(ref Duct duct, IList<Element> listTerminal, int connectType, int terminalConnType, XYZ direction = null)
		{
			if (duct == null || listTerminal == null)
			{
				return true;
			}
			this.DuctFittingSet(duct);
			this.ductConnectType = connectType;
			bool flag = false;
			IList<AutoConnectParameter> list = null;
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				duct = this.CheckDuctLength(duct, listTerminal);
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam = connectHelper.InitYJKElement(duct, listTerminal);
				IList<Connector> list2 = new List<Connector>();
				list2 = connectHelper.GetDuctConnectorEnd(duct);
				XYZ xyz = list2[0].Origin;
				XYZ origin = list2[1].Origin;
				XYZ xyz2 = direction;
				if (xyz2 == null)
				{
					xyz2 = -list2[0].CoordinateSystem.BasisZ;
				}
				list = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(xyz, origin, listParam), xyz, origin);
				IList<AutoConnectParameter> list3 = new List<AutoConnectParameter>();
				int num = 1;
				int i = 0;
				for (i = 0; i < list.Count; i++)
				{
					int j;
					for (j = i; j < list.Count; j++)
					{
						if (list[j].IndexOfDuct != num)
						{
							i = j - 1;
							break;
						}
						list3.Add(list[j]);
					}
					if (i != j - 1)
					{
						i = j - 1;
					}
					if (list3.Count > 0)
					{
						if (list3.Count > 1)
						{
							if (!connectHelper.IsElementAlign(list3))
							{
								List<Element> list4 = new List<Element>();
								foreach (AutoConnectParameter autoConnectParameter in list3)
								{
									list4.Add(document.GetElementById(autoConnectParameter.ElementId));
								}
								connectHelper.AlignElement(xyz, origin, list4);
							}
						}
						else if (list3[0].PositionFlag == 3 && list3[0].VectorToDuct.GetLength() < 0.33)
						{
							connectHelper.AlignElement(xyz, origin, document.GetElementById(list3[0].ElementId));
							list3[0].PositionFlag = 1;
						}
						else if (list3[0].PositionFlag == 1 && list3[0].VectorToDuct.GetLength() > 0.0001)
						{
							connectHelper.AlignElement(xyz, origin, document.GetElementById(list3[0].ElementId));
						}
						else if (list3[0].PositionFlag == 2)
						{
							connectHelper.AlignElement(xyz, origin, document.GetElementById(list3[0].ElementId));
						}
						if (list3[0].PositionFlag == 2)
						{
							this.ConnectDuctTerminalLoop2(document, duct, list3, terminalConnType);
						}
						else if (list3[0].PositionFlag == 1)
						{
							xyz = this.ConnectDuctTerminalLoop1(document, ref duct, list3, xyz, terminalConnType);
						}
						else if (list3[0].PositionFlag == 3)
						{
							IList<AutoConnectParameter> listParam2 = connectHelper.OrderByOfVector(list3);
							xyz = this.ConnectDuctTerminalLoop3(document, ref duct, listParam2, xyz, terminalConnType, xyz2);
						}
					}
					document.Regenerate();
					list3.Clear();
					num++;
					flag = true;
				}
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			return result;
		}

		public bool DuctConnectAirTerminalByRectangleN(ref Duct duct, IList<Element> listTerminal, int connectType, int terminalConnType, XYZ globalPnt, int alignType)
		{
			if (duct == null || listTerminal == null)
			{
				return true;
			}
			this.DuctFittingSet(duct);
			this.ductConnectType = connectType;
			bool flag = false;
			IList<AutoConnectParameter> list = null;
			bool result;
			try
			{
				Autodesk.Revit.DB.Document document = duct.Document;
				duct = this.CheckDuctLength(duct, listTerminal);
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam = connectHelper.InitYJKElement(duct, listTerminal);
				IList<Connector> list2 = new List<Connector>();
				list2 = connectHelper.GetDuctConnectorEnd(duct);
				Connector connector = (list2[0].Origin.DistanceTo(globalPnt) > list2[1].Origin.DistanceTo(globalPnt)) ? list2[1] : list2[0];
				XYZ xyz = list2[0].Origin;
				XYZ origin = list2[1].Origin;
				if (connector.Origin.DistanceTo(list2[0].Origin) > connector.Origin.DistanceTo(list2[1].Origin))
				{
					xyz = list2[0].Origin;
					origin = list2[1].Origin;
				}
				else
				{
					xyz = list2[1].Origin;
					origin = list2[0].Origin;
				}
				XYZ origin2 = connector.Origin;
				XYZ direction = -1.0 * connector.CoordinateSystem.BasisZ;
				list = connectHelper.PositionAssign(connectHelper.OrderByDistanceOfEnd(xyz, origin, listParam), xyz, origin);
				new ElementSet();
				IList<AutoConnectParameter> list3 = new List<AutoConnectParameter>();
				int num = 1;
				int i = 0;
				for (i = 0; i < list.Count; i++)
				{
					int j;
					for (j = i; j < list.Count; j++)
					{
						if (list[j].IndexOfDuct != num)
						{
							i = j - 1;
							break;
						}
						list3.Add(list[j]);
					}
					if (i != j - 1)
					{
						i = j - 1;
					}
					if (list3.Count > 0)
					{
						if (list3.Count > 1)
						{
							if (!connectHelper.IsElementAlign(list3))
							{
								List<Element> list4 = new List<Element>();
								foreach (AutoConnectParameter autoConnectParameter in list3)
								{
									list4.Add(document.GetElementById(autoConnectParameter.ElementId));
								}
								connectHelper.AlignElement(xyz, origin, list4);
							}
						}
						else if (list3[0].PositionFlag == 3 && list3[0].VectorToDuct.GetLength() < 0.33)
						{
							connectHelper.AlignElement(xyz, origin, document.GetElementById(list3[0].ElementId));
							list3[0].PositionFlag = 1;
						}
						else if (list3[0].PositionFlag == 1 && list3[0].VectorToDuct.GetLength() > 0.0001)
						{
							connectHelper.AlignElement(xyz, origin, document.GetElementById(list3[0].ElementId));
						}
						else if (list3[0].PositionFlag == 2)
						{
							connectHelper.AlignElement(xyz, origin, document.GetElementById(list3[0].ElementId));
						}
						if (list3[0].PositionFlag == 2)
						{
							this.ConnectDuctTerminalLoop2(document, duct, list3, terminalConnType);
						}
						else if (list3[0].PositionFlag == 1)
						{
							xyz = this.ConnectDuctTerminalLoop1(document, ref duct, list3, xyz, terminalConnType, origin2);
						}
						else if (list3[0].PositionFlag == 3)
						{
							IList<AutoConnectParameter> listParam2 = connectHelper.OrderByOfVector(list3);
							xyz = this.ConnectDuctTerminalLoop4(document, ref duct, listParam2, xyz, terminalConnType, origin2, alignType, direction);
						}
					}
					document.Regenerate();
					list3.Clear();
					num++;
					flag = true;
				}
				result = flag;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				flag = false;
				result = flag;
			}
			return result;
		}

		private void ConnectDuctTerminalLoop2(Autodesk.Revit.DB.Document doc, Duct duct, IList<AutoConnectParameter> listParam, int terminalConnType)
		{
			foreach (AutoConnectParameter autoConnectParameter in listParam)
			{
				this.DuctConnectAirTerminalElbow(duct, doc.GetElementById(autoConnectParameter.ElementId) as FamilyInstance, terminalConnType);
				doc.Regenerate();
			}
		}

		private XYZ ConnectDuctTerminalLoop1(Autodesk.Revit.DB.Document doc, ref Duct duct, IList<AutoConnectParameter> listParam, XYZ endPoint, int terminalConnType)
		{
			Duct duct2 = duct;
			XYZ xyz = endPoint;
			foreach (AutoConnectParameter autoConnectParameter in listParam)
			{
				XYZ xyz2 = xyz;
				Duct duct3 = duct2;
				if (this.ductConnectType == 1)
				{
					this.DuctConnectAirTerminalTee(ref duct3, doc.GetElementById(autoConnectParameter.ElementId) as FamilyInstance, ref xyz2, terminalConnType);
				}
				else
				{
					this.DuctConnectAirTerminalTakeOff(duct3, doc.GetElementById(autoConnectParameter.ElementId) as FamilyInstance);
				}
				xyz = xyz2;
				duct2 = duct3;
			}
			doc.Regenerate();
			duct = duct2;
			return xyz;
		}

		private XYZ ConnectDuctTerminalLoop1(Autodesk.Revit.DB.Document doc, ref Duct duct, IList<AutoConnectParameter> listParam, XYZ endPoint, int terminalConnType, XYZ origin)
		{
			Duct duct2 = duct;
			XYZ xyz = endPoint;
			foreach (AutoConnectParameter autoConnectParameter in listParam)
			{
				XYZ xyz2 = xyz;
				Duct duct3 = duct2;
				if (this.ductConnectType == 1)
				{
					this.DuctConnectAirTerminalTee(ref duct3, doc.GetElementById(autoConnectParameter.ElementId) as FamilyInstance, ref xyz2, terminalConnType, origin);
				}
				else
				{
					this.DuctConnectAirTerminalTakeOff(duct3, doc.GetElementById(autoConnectParameter.ElementId) as FamilyInstance);
				}
				xyz = xyz2;
				duct2 = duct3;
			}
			doc.Regenerate();
			duct = duct2;
			return xyz;
		}

		private XYZ ConnectDuctTerminalLoop3(Autodesk.Revit.DB.Document doc, ref Duct duct, IList<AutoConnectParameter> listParam, XYZ endPoint, int terminalConnType, XYZ direction = null)
		{
			Duct duct2 = duct;
			XYZ result = endPoint;
			int num = 0;
			for (int i = 0; i < listParam.Count; i++)
			{
				if (listParam[0].IndexOfElement == listParam[i].IndexOfElement)
				{
					num = i;
				}
			}
			if (num != 0)
			{
				FamilyInstance airTerminal = doc.GetElementById(listParam[0].ElementId) as FamilyInstance;
				FamilyInstance airTerminal2 = doc.GetElementById(listParam[num].ElementId) as FamilyInstance;
				IList<Duct> list = this.DuctConnectAirTerminalCross(ref duct2, airTerminal, airTerminal2, ref result, terminalConnType, direction);
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam2 = new List<AutoConnectParameter>();
				Duct duct3 = list[0];
				Duct duct4 = list[1];
				if (num > 1)
				{
					if (connectHelper.GetProjectOnDuct(list[0], doc.GetElementById(listParam[1].ElementId), 0.0001) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct3, listParam2, this.GetEndPointOfDuct(list[0], listParam[0].LocationPoint.Point), terminalConnType);
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct4, listParam2, this.GetEndPointOfDuct(list[1], listParam[num].LocationPoint.Point), terminalConnType);
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct4, listParam2, this.GetEndPointOfDuct(list[1], listParam[0].LocationPoint.Point), terminalConnType);
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct3, listParam2, this.GetEndPointOfDuct(list[0], listParam[num].LocationPoint.Point), terminalConnType);
					}
				}
				else if (num + 1 < listParam.Count)
				{
					if (connectHelper.GetProjectOnDuct(list[0], doc.GetElementById(listParam[num + 1].ElementId), 0.0001) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct3, listParam2, this.GetEndPointOfDuct(list[0], listParam[num].LocationPoint.Point), terminalConnType);
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct4, listParam2, this.GetEndPointOfDuct(list[1], listParam[num].LocationPoint.Point), terminalConnType);
					}
				}
			}
			else if (num == 0)
			{
				new ConnectHelper();
				Duct duct5 = null;
				if (this.ductConnectType == 1)
				{
					duct5 = this.DuctConnectAirTerminalTee(ref duct2, doc.GetElementById(listParam[0].ElementId) as FamilyInstance, ref result, terminalConnType);
				}
				else
				{
					duct5 = this.DuctConnectAirTerminalTakeOff(duct2, doc.GetElementById(listParam[0].ElementId) as FamilyInstance, true, terminalConnType);
				}
				IList<AutoConnectParameter> listParam3 = new List<AutoConnectParameter>();
				listParam3 = this.GetElementOutFirst(doc, listParam, num + 1);
				this.ConnectDuctTerminalLoop1(doc, ref duct5, listParam3, this.GetEndPointOfDuct(duct5, listParam[num].LocationPoint.Point), terminalConnType);
			}
			duct = duct2;
			return result;
		}

		private XYZ ConnectDuctTerminalLoop4(Autodesk.Revit.DB.Document doc, ref Duct duct, IList<AutoConnectParameter> listParam, XYZ endPoint, int terminalConnType, XYZ globePnt, int alignType, XYZ direction = null)
		{
			Duct duct2 = duct;
			XYZ result = endPoint;
			int num = 0;
			for (int i = 0; i < listParam.Count; i++)
			{
				if (listParam[0].IndexOfElement == listParam[i].IndexOfElement)
				{
					num = i;
				}
			}
			if (num != 0)
			{
				FamilyInstance airTerminal = doc.GetElementById(listParam[0].ElementId) as FamilyInstance;
				FamilyInstance airTerminal2 = doc.GetElementById(listParam[num].ElementId) as FamilyInstance;
				IList<Duct> list = this.DuctCrossConnTerminal(ref duct2, airTerminal, airTerminal2, ref result, terminalConnType, globePnt, alignType, direction);
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam2 = new List<AutoConnectParameter>();
				Duct duct3 = list[0];
				Duct duct4 = list[1];
				if (num > 1)
				{
					if (connectHelper.GetProjectOnDuct(list[0], doc.GetElementById(listParam[1].ElementId), 0.0001) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct3, listParam2, this.GetEndPointOfDuct(list[0], listParam[0].LocationPoint.Point), terminalConnType, globePnt);
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct4, listParam2, this.GetEndPointOfDuct(list[1], listParam[num].LocationPoint.Point), terminalConnType, globePnt);
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct4, listParam2, this.GetEndPointOfDuct(list[1], listParam[0].LocationPoint.Point), terminalConnType, globePnt);
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct3, listParam2, this.GetEndPointOfDuct(list[0], listParam[num].LocationPoint.Point), terminalConnType, globePnt);
					}
				}
				else if (num + 1 < listParam.Count)
				{
					if (connectHelper.GetProjectOnDuct(list[0], doc.GetElementById(listParam[num + 1].ElementId), 0.0001) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct3, listParam2, this.GetEndPointOfDuct(list[0], listParam[num].LocationPoint.Point), terminalConnType, globePnt);
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct4, listParam2, this.GetEndPointOfDuct(list[1], listParam[num].LocationPoint.Point), terminalConnType, globePnt);
					}
				}
			}
			else if (num == 0)
			{
				new ConnectHelper();
				Duct duct5 = null;
				if (this.ductConnectType == 1)
				{
					duct5 = this.DuctTeeConnTerminal(ref duct2, doc.GetElementById(listParam[0].ElementId) as FamilyInstance, ref result, terminalConnType, globePnt, alignType);
				}
				else
				{
					duct5 = this.DuctConnectAirTerminalTakeOff(duct2, doc.GetElementById(listParam[0].ElementId) as FamilyInstance, true, terminalConnType);
				}
				if (duct5 == null)
				{
					throw new Exception("风管缺少布管系统配置！");
				}
				IList<AutoConnectParameter> listParam3 = new List<AutoConnectParameter>();
				listParam3 = this.GetElementOutFirst(doc, listParam, num + 1);
				this.ConnectDuctTerminalLoop1(doc, ref duct5, listParam3, this.GetEndPointOfDuct(duct5, listParam[num].LocationPoint.Point), terminalConnType, globePnt);
			}
			duct = duct2;
			return result;
		}

		private XYZ ConnectDuctTerminalLoop3(Autodesk.Revit.DB.Document doc, ref Duct duct, IList<AutoConnectParameter> listParam, XYZ endPoint, int terminalConnType, XYZ globePnt, XYZ direction = null)
		{
			Duct duct2 = duct;
			XYZ result = endPoint;
			int num = 0;
			for (int i = 0; i < listParam.Count; i++)
			{
				if (listParam[0].IndexOfElement == listParam[i].IndexOfElement)
				{
					num = i;
				}
			}
			if (num != 0)
			{
				FamilyInstance airTerminal = doc.GetElementById(listParam[0].ElementId) as FamilyInstance;
				FamilyInstance airTerminal2 = doc.GetElementById(listParam[num].ElementId) as FamilyInstance;
				IList<Duct> list = this.DuctConnectAirTerminalCross(ref duct2, airTerminal, airTerminal2, ref result, terminalConnType, globePnt, direction);
				ConnectHelper connectHelper = new ConnectHelper();
				IList<AutoConnectParameter> listParam2 = new List<AutoConnectParameter>();
				Duct duct3 = list[0];
				Duct duct4 = list[1];
				if (num > 1)
				{
					if (connectHelper.GetProjectOnDuct(list[0], doc.GetElementById(listParam[1].ElementId), 0.0001) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct3, listParam2, this.GetEndPointOfDuct(list[0], listParam[0].LocationPoint.Point), terminalConnType, globePnt);
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct4, listParam2, this.GetEndPointOfDuct(list[1], listParam[num].LocationPoint.Point), terminalConnType, globePnt);
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct4, listParam2, this.GetEndPointOfDuct(list[1], listParam[0].LocationPoint.Point), terminalConnType, globePnt);
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct3, listParam2, this.GetEndPointOfDuct(list[0], listParam[num].LocationPoint.Point), terminalConnType, globePnt);
					}
				}
				else if (num + 1 < listParam.Count)
				{
					if (connectHelper.GetProjectOnDuct(list[0], doc.GetElementById(listParam[num + 1].ElementId), 0.0001) != null)
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct3, listParam2, this.GetEndPointOfDuct(list[0], listParam[num].LocationPoint.Point), terminalConnType, globePnt);
					}
					else
					{
						listParam2 = this.GetElementOutFirst(doc, listParam, num + 1);
						this.ConnectDuctTerminalLoop1(doc, ref duct4, listParam2, this.GetEndPointOfDuct(list[1], listParam[num].LocationPoint.Point), terminalConnType, globePnt);
					}
				}
			}
			else if (num == 0)
			{
				new ConnectHelper();
				Duct duct5 = null;
				if (this.ductConnectType == 1)
				{
					duct5 = this.DuctConnectAirTerminalTee(ref duct2, doc.GetElementById(listParam[0].ElementId) as FamilyInstance, ref result, terminalConnType, globePnt);
				}
				else
				{
					duct5 = this.DuctConnectAirTerminalTakeOff(duct2, doc.GetElementById(listParam[0].ElementId) as FamilyInstance, true, terminalConnType);
				}
				IList<AutoConnectParameter> listParam3 = new List<AutoConnectParameter>();
				listParam3 = this.GetElementOutFirst(doc, listParam, num + 1);
				this.ConnectDuctTerminalLoop1(doc, ref duct5, listParam3, this.GetEndPointOfDuct(duct5, listParam[num].LocationPoint.Point), terminalConnType, globePnt);
			}
			duct = duct2;
			return result;
		}

		private XYZ GetEndPointOfDuct(Duct duct, XYZ point)
		{
			XYZ result = new XYZ();
			IList<Connector> ductConnectorEnd = new ConnectHelper().GetDuctConnectorEnd(duct);
			if (ductConnectorEnd[0].Origin.DistanceTo(point) > ductConnectorEnd[1].Origin.DistanceTo(point))
			{
				result = ductConnectorEnd[1].Origin;
			}
			else
			{
				result = ductConnectorEnd[0].Origin;
			}
			return result;
		}

		private IList<AutoConnectParameter> GetElementOutFirst(Autodesk.Revit.DB.Document doc, IList<AutoConnectParameter> listElem, int iPosition)
		{
			IList<AutoConnectParameter> list = new List<AutoConnectParameter>();
			for (int i = iPosition; i < listElem.Count; i++)
			{
				if (i < listElem.Count)
				{
					if (listElem[i].IndexOfElement <= 1)
					{
						break;
					}
					list.Add(listElem[i]);
				}
			}
			return list;
		}

		public bool NewBeatchDuctEquipment(FamilyInstance familyInstance, IList<Duct> ducts)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts == null || ducts.Count < 1 || familyInstance == null)
				{
					throw new System.ArgumentNullException("Duct or Equipment", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				IList<Connector> hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)1);
				if (hyelementConnector == null || hyelementConnector.Count < 1)
				{
					throw new Exception("设备没有风管接口，请检查后连接");
				}
				IList<YJKPiping<Duct>> list = new List<YJKPiping<Duct>>();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				list = this.DuctsAndConnectorOrderByType(hyelementConnector, ducts, document);
				bool flag = list == null || list.Count <= 0 || this.DuctsConnect(list, document);
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
                if (subTransaction != null && 3 != (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool NewBeatchDuctEquipment(FamilyInstance familyInstance, IList<Duct> ducts, bool UseFlexDuct, double dFlexLength)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts == null || ducts.Count < 1 || familyInstance == null)
				{
					throw new System.ArgumentNullException("Duct or Equipment", "is null");
				}
				Autodesk.Revit.DB.Document document = ducts[0].Document;
				IList<Connector> hyelementConnector = new ConnectHelper().GetYJKElementConnector(familyInstance, (Autodesk.Revit.DB.Domain)1);
				if (hyelementConnector == null || hyelementConnector.Count < 1)
				{
					throw new Exception("不支持此类型风机的连接！");
				}
				IList<YJKPiping<Duct>> list = new List<YJKPiping<Duct>>();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				list = this.DuctsAndConnectorOrderByType(hyelementConnector, ducts, document, UseFlexDuct, dFlexLength);
				bool flag = list == null || list.Count <= 0 || this.DuctsConnect(list, document);
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
                if (subTransaction != null && 3 != (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private IList<YJKPiping<Duct>> DuctsAndConnectorOrderByType(IList<Connector> connectors, IList<Duct> ducts, Autodesk.Revit.DB.Document doc)
		{
			IList<YJKPiping<Duct>> list = new List<YJKPiping<Duct>>();
			List<Connector> list2 = new List<Connector>();
			foreach (Connector connector in connectors)
			{
				XYZ origin = connector.Origin;
				FamilyInstance fi = connector.Owner as FamilyInstance;
				bool flag = false;
				foreach (Duct duct in ducts)
				{
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
					if (connector.DuctSystemType == hypiping.GetConnectorPipingSystemType().ductSystemType && !connector.IsConnected)
					{
						if (connector.Shape == hypiping.StartConnector.Connector.Shape)
						{
							XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(connector);
							XYZ connectorDirectionPoint2 = this.GetConnectorDirectionPoint(connector, 5);
							YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(YJKRevitTools.NewDuct(hypiping.Piping, connectorDirectionPoint, connectorDirectionPoint2));
							Connector closeConnector = hypiping2.GetCloseConnector(connectorDirectionPoint, false);
							FlexDuctType flexDuctType = YJKRevitTools.GetContainNameElement(YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_FlexDuctCurves), YJKRevitTools.GetConnectorSharpName(connector)) as FlexDuctType;
							FlexDuct flexDuct = doc.Create.NewFlexDuct(connector, closeConnector, flexDuctType);
							this.SetFlexDuctByDuct(connector, flexDuct);
							this.DuctConnectFamilyInstance(fi, flexDuct);
							list.Add(hypiping);
							list.Add(hypiping2);
							ducts.Remove(duct);
							flag = true;
							break;
						}
						Connector connector2 = hypiping.GetCloseConnector(connector.Origin, false);
						if (connector2.IsConnected)
						{
							connector2 = hypiping.GetDistanceConnector(connector.Origin);
						}
						if (connector2 != null && !connector2.IsConnected)
						{
							FlexDuctType flexDuctType2 = YJKRevitTools.GetContainNameElement(YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_FlexDuctCurves), YJKRevitTools.GetConnectorSharpName(connector)) as FlexDuctType;
							FlexDuct flexDuct2 = doc.Create.NewFlexDuct(connector, connector2, flexDuctType2);
							this.SetFlexDuctByDuct(connector, flexDuct2);
							ducts.Remove(duct);
							flag = true;
							break;
						}
					}
				}
				if (!flag)
				{
					list2.Add(connector);
				}
			}
			foreach (Connector connector3 in list2)
			{
				XYZ origin2 = connector3.Origin;
				FamilyInstance fi = connector3.Owner as FamilyInstance;
				foreach (Duct duct2 in ducts)
				{
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct2);
					if (connector3.Shape != hypiping3.StartConnector.Connector.Shape)
					{
						Connector connector4 = hypiping3.GetCloseConnector(connector3.Origin, false);
						if (connector4.IsConnected)
						{
							connector4 = hypiping3.GetDistanceConnector(connector3.Origin);
						}
						if (connector4 != null && !connector4.IsConnected)
						{
							FlexDuctType flexDuctType3 = YJKRevitTools.GetContainNameElement(YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_FlexDuctCurves), YJKRevitTools.GetConnectorSharpName(connector3)) as FlexDuctType;
							FlexDuct flexDuct3 = doc.Create.NewFlexDuct(connector3, connector4, flexDuctType3);
							this.SetFlexDuctByDuct(connector3, flexDuct3);
							ducts.Remove(duct2);
							break;
						}
					}
					else if (!connector3.IsConnected)
					{
						XYZ connectorDirectionPoint3 = this.GetConnectorDirectionPoint(connector3);
						XYZ xyzbyDistance = YJKRevitTools.GetXYZByDistance(origin2, connectorDirectionPoint3, 0.4);
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(YJKRevitTools.NewDuct(hypiping3.Piping, connectorDirectionPoint3, xyzbyDistance));
						Connector closeConnector2 = hypiping4.GetCloseConnector(connectorDirectionPoint3, false);
						FlexDuctType flexDuctType4 = YJKRevitTools.GetContainNameElement(YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_FlexDuctCurves), YJKRevitTools.GetConnectorSharpName(connector3)) as FlexDuctType;
						FlexDuct flexDuct4 = doc.Create.NewFlexDuct(connector3, closeConnector2, flexDuctType4);
						this.SetFlexDuctByDuct(connector3, flexDuct4);
						this.DuctConnectFamilyInstance(fi, flexDuct4);
						list.Add(hypiping3);
						list.Add(hypiping4);
						ducts.Remove(duct2);
						break;
					}
				}
			}
			return list;
		}

		private IList<YJKPiping<Duct>> DuctsAndConnectorOrderByType(IList<Connector> connectors, IList<Duct> ducts, Autodesk.Revit.DB.Document doc, bool UseFlexDuct, double dFlexLength)
		{
			IList<YJKPiping<Duct>> list = new List<YJKPiping<Duct>>();
			FamilyInstance familyInstance = null;
			foreach (Connector connector in connectors)
			{
				if (!connector.IsConnected)
				{
					XYZ origin = connector.Origin;
					familyInstance = (connector.Owner as FamilyInstance);
					foreach (Duct duct in ducts)
					{
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
						Connector connector2 = hypiping.GetCloseConnector(connector.Origin, false);
						if (connector2.IsConnected)
						{
							connector2 = hypiping.GetDistanceConnector(connector.Origin);
						}
						if (this.CompareTwoConnectorDir(connector, connector2))
						{
							if (UseFlexDuct)
							{
								if (connector2 != null && !connector2.IsConnected)
								{
									if (!this.CompareTwoConnectorSharp(connector, connector2))
									{
										FamilySymbol ductFittingFamilySymbolByFamilyName = this.GetDuctFittingFamilySymbolByFamilyName("天圆地方", doc);
										List<XYZ> list2 = new List<XYZ>();
										list2.Add(new XYZ(0.0, 0.0, 0.0));
										foreach (object obj in new ConnectHelper().LayFamilyInstance(list2, ductFittingFamilySymbolByFamilyName, doc.ActiveView.GenLevel, 0, doc))
										{
											Element element = (Element)obj;
											doc.Delete(element.Id);
										}
										doc.Regenerate();
										FamilySymbol fd = this.setFlexParameter(doc, familyInstance.Symbol.FamilyName, connector);
										bool bIsRiserDuct = this.IsRiserDuct(hypiping.Piping);
										this.LayFlex(bIsRiserDuct, connector, doc, fd, null, familyInstance, hypiping, true, ductFittingFamilySymbolByFamilyName, connector2.Origin, connector2, dFlexLength);
										ducts.Remove(duct);
										break;
									}
									if (familyInstance.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).AsDouble() == hypiping.GetOffSet())
									{
										FamilySymbol fd2 = this.setFlexParameter(doc, familyInstance.Symbol.FamilyName, connector);
										bool bIsRiserDuct2 = this.IsRiserDuct(hypiping.Piping);
										this.LayFlex(bIsRiserDuct2, connector, doc, fd2, null, familyInstance, hypiping, false, null, connector2.Origin, connector2, dFlexLength);
										ducts.Remove(duct);
										break;
									}
									FamilySymbol fd3 = this.setFlexParameter(doc, familyInstance.Symbol.FamilyName, connector);
									bool bIsRiserDuct3 = this.IsRiserDuct(hypiping.Piping);
									this.LayFlex(bIsRiserDuct3, connector, doc, fd3, null, familyInstance, hypiping, false, null, connector2.Origin, connector2, dFlexLength);
									ducts.Remove(duct);
									break;
								}
							}
							else if (connector2 != null && !connector2.IsConnected)
							{
								if (this.CompareTwoConnectorSharpAndRadius(connector, connector2) && familyInstance.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).AsDouble() == hypiping.GetOffSet())
								{
									XYZ origin2 = connector.Origin;
									XYZ connectorDirectionPoint = this.GetConnectorDirectionPoint(connector, 2);
									YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(YJKRevitTools.NewDuct(hypiping.Piping, origin2, connectorDirectionPoint));
									hypiping2.GetCloseConnector(origin2, false).ConnectTo(connector);
									list.Add(hypiping);
									list.Add(hypiping2);
									ducts.Remove(duct);
									break;
								}
								YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_DuctFitting);
								string familyName = null;
								if (connector.Shape == connector2.Shape)
								{
									if (connector.Shape == ConnectorSharp.Rect)
									{
										familyName = "矩形变径";
									}
									else if (connector.Shape == ConnectorSharp.Round)
									{
										familyName = "圆形变径";
									}
								}
								else
								{
									familyName = "天圆地方";
								}
								FamilySymbol ductFittingFamilySymbolByFamilyName2 = this.GetDuctFittingFamilySymbolByFamilyName(familyName, doc);
								if (hypiping.Shape == connector.Shape)
								{
									hypiping.ChangeFirstTransition(ductFittingFamilySymbolByFamilyName2);
								}
								else
								{
									hypiping.ChangeFirstTYDF(ductFittingFamilySymbolByFamilyName2);
								}
								doc.Regenerate();
								doc.Create.NewTransitionFitting(connector2, connector);
								ElementTransformUtils.MoveElement(doc, familyInstance.Id, connector.CoordinateSystem.BasisZ);
								ElementTransformUtils.MoveElement(doc, familyInstance.Id, -connector.CoordinateSystem.BasisZ);
								doc.Regenerate();
								ducts.Remove(duct);
								break;
							}
						}
					}
				}
			}
			return list;
		}

		private bool IsRiserDuct(Duct duct)
		{
			LocationCurve locationCurve = duct.Location as LocationCurve;
			double value = locationCurve.Curve.GetEndPoint(0).X - locationCurve.Curve.GetEndPoint(1).X;
			double value2 = locationCurve.Curve.GetEndPoint(0).Y - locationCurve.Curve.GetEndPoint(1).Y;
			return Math.Abs(value) < 0.0001 && Math.Abs(value2) < 0.0001;
		}

		private void LayFlex(bool bIsRiserDuct, Connector conn, Autodesk.Revit.DB.Document doc, FamilySymbol fd, FamilyInstance fdi, FamilyInstance fi, YJKPiping<Duct> hyduct, bool addTYDF, FamilySymbol fs, XYZ ptInsert, Connector ductconn, double length)
		{
			ConnectHelper connectHelper = new ConnectHelper();
			Line line = null;
			if (bIsRiserDuct)
			{
				line = doc.CreatYJKLine(conn.Origin, new XYZ(0.0, 1.0, 0.0), false);
			}
			else
			{
				line = doc.CreatYJKLine(conn.Origin, new XYZ(0.0, 0.0, 1.0), false);
			}
			new List<XYZ>().Add(conn.Origin);
			FamilyInstanceCreationData item = new FamilyInstanceCreationData(conn.Origin, fd, conn.Owner, 0);
			List<FamilyInstanceCreationData> list = new List<FamilyInstanceCreationData>();
			list.Add(item);
			fdi = (doc.GetElement(doc.Create.NewFamilyInstances2(list).ToList<ElementId>().FirstOrDefault<ElementId>()) as FamilyInstance);
			doc.Regenerate();
			foreach (Connector flexConn in YJKRevitTools.GetElementAllConnector(fdi))
			{
				this.SetFlexFittingSize(flexConn, conn);
			}
			Parameter parameter = fdi.GetParameter("风管直径");
			parameter = fdi.GetParameter("软连接长度");
			if (parameter != null && !parameter.IsReadOnly)
			{
				fdi.GetParameter("软连接长度").Set(NumbericTools.mmToFeet(length));
			}
			XYZ xyz = (hyduct.EndConnector.Origin - hyduct.StartConnector.Origin).Normalize();
			double num = xyz.AngleTo(new XYZ(1.0, 0.0, 0.0));
			ElementTransformUtils.RotateElement(doc, fdi.Id, line, -num);
			IList<Connector> hyelementConnector = connectHelper.GetYJKElementConnector(fdi, (Autodesk.Revit.DB.Domain)1);
			if (!xyz.IsAlmostEqualTo(hyelementConnector[0].CoordinateSystem.BasisZ, 0.0001) && !xyz.IsAlmostEqualTo(-hyelementConnector[0].CoordinateSystem.BasisZ, 0.0001))
			{
				num = Math.PI*2 - 2.0 * num;
				ElementTransformUtils.RotateElement(doc, fdi.Id, line, -num);
			}
			if (addTYDF)
			{
				hyduct.ChangeFirstTYDF(fs);
				hyelementConnector.GetNearestConnector(ptInsert).ConnectTo(ductconn);
				hyelementConnector.GetFarestConnector(ptInsert).ConnectTo(conn);
				doc.Create.NewTransitionFitting(ductconn, hyelementConnector.GetNearestConnector(ptInsert));
				ElementTransformUtils.MoveElement(doc, fdi.Id, new XYZ(0.0, 0.0, 1.0));
				ElementTransformUtils.MoveElement(doc, fdi.Id, new XYZ(0.0, 0.0, -1.0));
			}
			else
			{
				hyelementConnector.GetNearestConnector(ptInsert).ConnectTo(ductconn);
				hyelementConnector.GetFarestConnector(ptInsert).ConnectTo(conn);
				doc.Create.NewTransitionFitting(ductconn, hyelementConnector.GetNearestConnector(ptInsert));
				ElementTransformUtils.MoveElement(doc, fdi.Id, new XYZ(0.0, 0.0, 1.0));
				ElementTransformUtils.MoveElement(doc, fdi.Id, new XYZ(0.0, 0.0, -1.0));
			}
			doc.Regenerate();
		}

		private void SetFlexFittingSize(Connector flexConn, Connector fanConn)
		{
			string connectorSharpName = YJKRevitTools.GetConnectorSharpName(flexConn);
			string connectorSharpName2 = YJKRevitTools.GetConnectorSharpName(fanConn);
			if (connectorSharpName != connectorSharpName2)
			{
				return;
			}
			if (connectorSharpName == "圆形")
			{
				flexConn.Radius = fanConn.Radius;
				return;
			}
			flexConn.Width = fanConn.Width;
			flexConn.Height = fanConn.Height;
		}

		private bool BeExsitParameterName(FamilyInstance fi, string paramName)
		{
			Parameter parameter = fi.GetParameter(paramName);
			return parameter == null || parameter.IsReadOnly || (int)parameter.StorageType != 2;
		}

		private FamilySymbol setFlexParameter(Autodesk.Revit.DB.Document doc, string fanParamName, Connector conn)
		{
			new SpaceFamilyDataReader();
			string familyName = "";
			string symbolName = "";
			if (fanParamName == "离心式管道风机" || YJKRevitTools.GetConnectorSharpName(conn) == "矩形")
			{
				familyName = "矩形软连接";
				symbolName = "标准";
			}
			else if (fanParamName == "轴流式管道风机" || YJKRevitTools.GetConnectorSharpName(conn) == "圆形")
			{
				familyName = "圆形软连接";
				symbolName = "标准";
			}
			return this.GetDuctFittingFamilySymbolByFamilyNameAndSymbol(familyName, symbolName, doc);
		}

		private FamilySymbol GetDuctFittingFamilySymbolByFamilyNameAndSymbol(string FamilyName, string SymbolName, Autodesk.Revit.DB.Document doc)
		{
			FamilySymbol familySymbol = null;
			foreach (Element element in YJKElementFilter.GetElementsOfType(doc, typeof(FamilySymbol), BuiltInCategory.OST_DuctAccessory))
			{
				if ((element as FamilySymbol).Family.Name.Contains(FamilyName))
				{
					familySymbol = (element as FamilySymbol);
				}
			}
			if (familySymbol == null)
			{
				SubTransaction subTransaction = null;
				try
				{
					string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), "fitting", FamilyName + ".rfa");
					subTransaction = new SubTransaction(doc);
					subTransaction.Start();
					FamilySymbol familySymbol2;
					doc.LoadFamilySymbol(text, SymbolName, out familySymbol2);
					familySymbol = familySymbol2;
					subTransaction.Commit();
				}
				catch (Exception ex)
				{
					ex.ToString();
				}
				finally
				{
					if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
					{
						subTransaction.RollBack();
					}
				}
			}
			return familySymbol;
		}

		private double GetFittingLength(FamilyInstance fi)
		{
			double num = 0.0;
			Parameter parameter = fi.GetParameter("风管长度");
			if (parameter != null)
			{
				num += parameter.AsDouble();
			}
			Parameter parameter2 = fi.GetParameter("法兰厚度 1");
			if (parameter2 != null)
			{
				num += parameter2.AsDouble();
			}
			Parameter parameter3 = fi.GetParameter("法兰厚度 2");
			if (parameter3 != null)
			{
				num += parameter3.AsDouble();
			}
			return num;
		}

		private bool CompareTwoConnectorDir(Connector conn1, Connector conn2)
		{
			bool result = false;
			XYZ basisZ = conn1.CoordinateSystem.BasisZ;
			XYZ basisZ2 = conn2.CoordinateSystem.BasisZ;
			double num = basisZ.AngleTo(basisZ2);
			if (Math.Abs(num - Math.PI*.5) < 0.001)
			{
				XYZ point = ((conn1.Owner as FamilyInstance).Location as LocationPoint).Point;
				if (conn1.Origin.DistanceTo(conn2.Origin) < conn2.Origin.DistanceTo(point))
				{
					result = true;
				}
			}
			else if (Math.Abs(num - Math.PI) < 0.001)
			{
				result = true;
			}
			return result;
		}

		private bool CompareTwoConnectorSharp(Connector conn1, Connector conn2)
		{
			bool result = true;
			if (conn1.Shape != conn2.Shape)
			{
				result = false;
			}
			return result;
		}

		private bool CompareTwoConnectorSharpAndRadius(Connector conn1, Connector conn2)
		{
			bool result = true;
			if (conn1.Shape != conn2.Shape)
			{
				result = false;
			}
			else if (conn1.Shape == ConnectorSharp.Rect || conn1.Shape == ConnectorSharp.Oval)
			{
				if (conn1.Width != conn2.Width || conn1.Height != conn2.Height)
				{
					result = false;
				}
			}
			else if (conn1.Shape == ConnectorSharp.Round && conn1.Radius != conn2.Radius)
			{
				result = false;
			}
			return result;
		}

		private FamilySymbol GetDuctFittingFamilySymbolByFamilyName(string FamilyName, Autodesk.Revit.DB.Document doc)
		{
			FamilySymbol familySymbol = null;
			foreach (Element element in YJKElementFilter.GetElementsOfType(doc, typeof(FamilySymbol), BuiltInCategory.OST_DuctFitting))
			{
				if ((element as FamilySymbol).Family.Name.Contains(FamilyName))
				{
					familySymbol = (element as FamilySymbol);
				}
			}
			if (familySymbol == null)
			{
				Family family = null;
				SubTransaction subTransaction = null;
				try
				{
					string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), "Fitting", FamilyName + ".rfa");
					subTransaction = new SubTransaction(doc);
					subTransaction.Start();
					doc.LoadFamily(text, out family);
					subTransaction.Commit();
				}
				catch (Exception ex)
				{
					ex.ToString();
				}
				finally
				{
					if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
					{
						subTransaction.RollBack();
					}
				}
				if (family != null)
				{
					using (List<FamilySymbol>.Enumerator enumerator2 = family.GetSymbols().GetEnumerator())
					{
						if (enumerator2.MoveNext())
						{
							familySymbol = enumerator2.Current;
						}
					}
				}
			}
			return familySymbol;
		}

		private FamilyInstance AddTransitionFitting(Duct duct1, Duct duct2)
		{
            Autodesk.Revit.DB.Document document = duct1.Document;
			foreach (object obj in duct1.ConnectorManager.Connectors)
			{
				Connector connector = (Connector)obj;
				foreach (object obj2 in duct2.ConnectorManager.Connectors)
				{
					Connector connector2 = (Connector)obj2;
					if (connector.Origin.IsAlmostEqualTo(connector2.Origin))
					{
						return document.Create.NewTransitionFitting(connector, connector2);
					}
				}
			}
			return null;
		}

		private void ConnectToFamilyInstrance(FamilyInstance fi, Connector conn)
		{
			IEnumerator enumerator = fi.MEPModel.ConnectorManager.UnusedConnectors.GetEnumerator();
			{
				if (enumerator.MoveNext())
				{
					((Connector)enumerator.Current).ConnectTo(conn);
				}
			}
		}

		private bool SetDuctByConn(ref Duct duct, Connector conn)
		{
			foreach (object obj in duct.ConnectorManager.Connectors)
			{
				Connector connector = (Connector)obj;
				if (connector.Shape != conn.Shape)
				{
					return false;
				}
				if (conn.Shape == ConnectorSharp.Round)
				{
					connector.Radius = conn.Radius;
				}
				else if (conn.Shape == ConnectorSharp.Rect || conn.Shape == ConnectorSharp.Oval)
				{
					connector.Width = conn.Width;
					connector.Height = conn.Height;
				}
			}
			return true;
		}

		private void DuctConnectFamilyInstance(FamilyInstance fi, Duct duct)
		{
			ConnectHelper connectHelper = new ConnectHelper();
			IList<Connector> ductConnectorEnd = connectHelper.GetDuctConnectorEnd(duct);
			IList<Connector> hyelementConnector = connectHelper.GetYJKElementConnector(fi, (Autodesk.Revit.DB.Domain)1);
			bool flag = false;
			foreach (Connector connector in ductConnectorEnd)
			{
				foreach (Connector connector2 in hyelementConnector)
				{
					if (connector.Origin.IsAlmostEqualTo(connector2.Origin))
					{
						connector.ConnectTo(connector2);
						flag = true;
						break;
					}
				}
				if (flag)
				{
					break;
				}
			}
		}

		private Connector DuctConnectFamilyInstance(FamilyInstance fi, FlexDuct duct)
		{
			Connector result = null;
			ConnectHelper connectHelper = new ConnectHelper();
			IList<Connector> flexDuctConnectorEnd = connectHelper.GetFlexDuctConnectorEnd(duct);
			IList<Connector> hyelementConnector = connectHelper.GetYJKElementConnector(fi, (Autodesk.Revit.DB.Domain)1);
			foreach (Connector connector in flexDuctConnectorEnd)
			{
				bool flag = false;
				foreach (Connector connector2 in hyelementConnector)
				{
					if (connector.Origin.IsAlmostEqualTo(connector2.Origin) && !connector.IsConnected && !connector2.IsConnected)
					{
						connector.ConnectTo(connector2);
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					result = connector;
				}
			}
			return result;
		}

		private void DuctConnectFlexDuct(Duct duct, FlexDuct fDuct)
		{
			IEnumerable<Connector> flexDuctConnectorEnd = new ConnectHelper().GetFlexDuctConnectorEnd(fDuct);
			bool flag = false;
			foreach (Connector connector in flexDuctConnectorEnd)
			{
				foreach (object obj in duct.ConnectorManager.Connectors)
				{
					Connector connector2 = (Connector)obj;
					if (connector.Origin.IsAlmostEqualTo(connector2.Origin) && !connector.IsConnected && !connector2.IsConnected)
					{
						connector.ConnectTo(connector2);
						flag = true;
						break;
					}
				}
				if (flag)
				{
					break;
				}
			}
		}

		private void SetFlexDuctByDuct(Connector conn, FlexDuct flexDuct)
		{
			List<Connector> connectorAll = YJKRevitTools.GetConnectorAll(flexDuct);
			if (connectorAll != null && connectorAll.Count > 0 && connectorAll[0].Shape == conn.Shape)
			{
				foreach (Connector connector in connectorAll)
				{
					if (conn.Shape == ConnectorSharp.Rect || conn.Shape == ConnectorSharp.Oval)
					{
						connector.Width = conn.Width;
						connector.Height = conn.Height;
					}
					else if (conn.Shape == ConnectorSharp.Round)
					{
						connector.Radius = conn.Radius;
					}
				}
			}
		}

		public bool DuctATerminalSharpCheck(Duct duct, Connector conn)
		{
			bool result = true;
			if (new YJKPiping<Duct>(duct).StartConnector.Connector.Shape != conn.Shape)
			{
				result = false;
			}
			return result;
		}

		public bool DuctATerminalSharpCheck(Duct duct, Connector conn, Connector conn2)
		{
			bool result = true;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			if (hypiping.StartConnector.Connector.Shape != conn.Shape || hypiping.StartConnector.Connector.Shape != conn2.Shape)
			{
				result = false;
			}
			return result;
		}

		private int ductConnectType = 1;

		public enum DuctTeeConnStyle
		{
			TShapeTee,
			RoundAngleTee,
			RightAngleTee,
			IntubationTee,
			YShapeTee,
			CommonTee
		}
	}
}
